Skip to content

Commit

Permalink
Fix routing parameters filled in incorrectly using mapping property n…
Browse files Browse the repository at this point in the history
…ames
  • Loading branch information
c-v-c-v committed May 8, 2024
1 parent 2f704f9 commit 75f49ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/DataPipes/FillRouteParameterPropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function handle(
continue;
}

if (! $attribute->replaceWhenPresentInBody && array_key_exists($dataProperty->name, $properties)) {
// if inputMappedName exists, use it first
$name = $dataProperty->inputMappedName ?: $dataProperty->name;
if (! $attribute->replaceWhenPresentInBody && array_key_exists($name, $properties)) {
continue;
}

Expand All @@ -41,7 +43,12 @@ public function handle(
continue;
}

$properties[$dataProperty->name] = $this->resolveValue($dataProperty, $attribute, $parameter);
$properties[$name] = $this->resolveValue($dataProperty, $attribute, $parameter);

// keep the original property name
if ($name !== $dataProperty->name) {
$properties[$dataProperty->name] = $properties[$name];
}
}

return $properties;
Expand Down
15 changes: 14 additions & 1 deletion tests/FillRouteParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

use Spatie\LaravelData\Attributes\MapInputName;
use function Pest\Laravel\mock;

use Spatie\LaravelData\Attributes\FromRouteParameter;
Expand Down Expand Up @@ -85,6 +86,10 @@
public string $tag;
#[FromRouteParameterProperty('something', 'rows.*.total')]
public array $totals;

#[FromRouteParameter('user_id')]
#[MapInputName('user_id')]
public int $userId;
};

$something = [
Expand All @@ -102,6 +107,7 @@

$requestMock = mock(Request::class);
$requestMock->expects('route')->with('something')->times(4)->andReturns($something);
$requestMock->expects('route')->with('user_id')->once()->andReturns(1);
$requestMock->expects('toArray')->andReturns([]);

$data = $dataClass::from($requestMock);
Expand All @@ -110,6 +116,7 @@
expect($data->foo)->toEqual('bar');
expect($data->tag)->toEqual('foo');
expect($data->totals)->toEqual([10, 20, 30]);
expect($data->userId)->toEqual(1);
});

it('replaces properties when route parameter properties exist', function () {
Expand All @@ -118,6 +125,10 @@
public string $name;
#[FromRouteParameterProperty('bar')]
public string $slug;

#[FromRouteParameter('user_id')]
#[MapInputName('user_id')]
public int $userId;
};

$foo = 'Foo Lighters';
Expand All @@ -126,12 +137,14 @@
$requestMock = mock(Request::class);
$requestMock->expects('route')->with('foo')->once()->andReturns($foo);
$requestMock->expects('route')->with('bar')->once()->andReturns($bar);
$requestMock->expects('toArray')->andReturns(['name' => 'Loo Cleaners', 'slug' => 'loo-cleaners']);
$requestMock->expects('route')->with('user_id')->once()->andReturns(2);
$requestMock->expects('toArray')->andReturns(['name' => 'Loo Cleaners', 'slug' => 'loo-cleaners', 'user_id' => 1]);

$data = $dataClass::from($requestMock);

expect($data->name)->toEqual('Foo Lighters');
expect($data->slug)->toEqual('foo-lighters');
expect($data->userId)->toEqual(2);
});

it('skips replacing properties when route parameter properties exist and replacing is disabled', function () {
Expand Down

0 comments on commit 75f49ec

Please sign in to comment.