Skip to content

Commit 881d18e

Browse files
authored
[Bugfix] Add support for model bindings in route params (#506)
Check for url routable model when resolving model bindings in route parameters.
1 parent 7d1101a commit 881d18e

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Api/Url.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ public function replace(iterable $parameters): self
103103
$copy = clone $this;
104104

105105
foreach ($parameters as $key => $value) {
106-
$copy->namespace = \str_replace('{' . $key . '}', $value, $copy->namespace);
106+
$routeParamValue = $value;
107+
108+
if ($value instanceof UrlRoutable) {
109+
$routeParamValue = $value->getRouteKey();
110+
}
111+
112+
$copy->namespace = \str_replace('{' . $key . '}', $routeParamValue, $copy->namespace);
107113
}
108114

109115
return $copy;

tests/lib/Integration/Routing/RouteParameterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CloudCreativity\LaravelJsonApi\Routing\RouteRegistrar;
2121
use CloudCreativity\LaravelJsonApi\Tests\Integration\TestCase;
2222
use DummyApp\Post;
23+
use DummyApp\User;
2324
use Illuminate\Support\Arr;
2425

2526
/**
@@ -77,4 +78,16 @@ public function testManual(): void
7778
Arr::get($data, 'data.links.self')
7879
);
7980
}
81+
82+
public function testModelBinding(): void
83+
{
84+
$post = factory(Post::class)->create();
85+
$user = factory(User::class)->create();
86+
$data = json_api(null, null, ['tenant' => $user])->encoder()->serializeData($post);
87+
88+
$this->assertSame(
89+
url('/foo/2/api/posts', $post),
90+
Arr::get($data, 'data.links.self')
91+
);
92+
}
8093
}

0 commit comments

Comments
 (0)