Skip to content

Commit 99c2121

Browse files
authored
fix: Possible empty part of Route when path_param is not mandatory (#992)
* Fix the possible empty part of Route when path_param is not mandatory * Add test for #915
1 parent a6c2836 commit 99c2121

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Extracting/Shared/UrlParamsNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ public static function normalizeParameterNamesInRouteUri(Route $route, Reflectio
6262
if (!$alreadyFoundResourceParam) {
6363
// This is the first resource param (from the end).
6464
// We set it to `params/{id}` (or whatever field it's bound to)
65-
$replaceWith = ["$pluralResource/{{$binding}}", "$pluralResource/{{$binding}?}"];
65+
$replaceWith = [
66+
"$pluralResource/{{$binding}}",
67+
"$pluralResource/{{$binding}}",
68+
"$pluralResource/{{$binding}?}",
69+
"$pluralResource/{{$binding}?}",
70+
];
6671
$alreadyFoundResourceParam = true;
6772
} else {
6873
// Other resource parameters will be `params/{<param>_id}`

tests/Strategies/UrlParameters/GetFromLaravelAPITest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ public function can_infer_from_model_even_if_not_bound()
141141
$property->setValue($this->app, $oldNamespace);
142142
}
143143

144+
/** @test */
145+
public function can_infer_correct_uri_from_route_with_optional_parameter_and_named_resource_routename()
146+
{
147+
$endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
148+
$e->method = new \ReflectionMethod(TestController::class, 'dummy');
149+
$e->route = app(Router::class)
150+
->addRoute(['GET'], "/users/{user?}/show", ['uses' => [TestController::class, 'dummy']])
151+
->name('users.show')
152+
;
153+
$e->uri = UrlParamsNormalizer::normalizeParameterNamesInRouteUri($e->route, $e->method);
154+
155+
});
156+
157+
$this->assertEquals('users/{id?}/show', $endpoint->uri);
158+
}
159+
144160
protected function endpointForRoute($path, $controller, $method): ExtractedEndpointData
145161
{
146162
return $this->endpoint(function (ExtractedEndpointData $e) use ($path, $method, $controller) {

0 commit comments

Comments
 (0)