Skip to content

Commit b9f1655

Browse files
voronkovichnicolas-grekas
authored andcommitted
[Routing] Fix: annotation loader ignores method's default values
1 parent 319f600 commit b9f1655

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Loader/AnnotationClassLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
196196
continue;
197197
}
198198
foreach ($paths as $locale => $path) {
199-
if (false !== strpos($path, sprintf('{%s}', $param->name))) {
199+
if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) {
200200
$defaults[$param->name] = $param->getDefaultValue();
201201
break;
202202
}

Tests/Fixtures/AnnotationFixtures/DefaultValueController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ class DefaultValueController
1212
public function action($default = 'value')
1313
{
1414
}
15+
16+
/**
17+
* @Route("/hello/{name<\w+>}", name="hello_without_default")
18+
* @Route("/hello/{name<\w+>?Symfony}", name="hello_with_default")
19+
*/
20+
public function hello(string $name = 'World')
21+
{
22+
}
1523
}

Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ public function testLocalizedPathRoutesWithExplicitPathPropety()
136136
public function testDefaultValuesForMethods()
137137
{
138138
$routes = $this->loader->load(DefaultValueController::class);
139-
$this->assertCount(1, $routes);
139+
$this->assertCount(3, $routes);
140140
$this->assertEquals('/{default}/path', $routes->get('action')->getPath());
141141
$this->assertEquals('value', $routes->get('action')->getDefault('default'));
142+
$this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
143+
$this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
142144
}
143145

144146
public function testMethodActionControllers()

0 commit comments

Comments
 (0)