Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 25db9e2

Browse filesBrowse files
committed
bug #30825 [Routing] Fix: annotation loader ignores method's default values (voronkovich)
This PR was merged into the 4.2 branch. Discussion ---------- [Routing] Fix: annotation loader ignores method's default values | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? |no | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | no In some cases annotation loader ignores method param's default values. For example this code won't work as expected: ```php /** * @route("/hello/{name<\w++>}", methods="GET", name="hello") */ public function hello(Request $request, string $name = 'World'): Response { // If you try to open "/hello" path an exception (No route found for "GET /hello") will be thrown. return $this->json([ 'hello' => \sprintf('Hello, %s!', $name), ]); } ``` Commits ------- 9b37793 [Routing] Fix: annotation loader ignores method's default values
2 parents a362b8b + 9b37793 commit 25db9e2
Copy full SHA for 25db9e2

File tree

3 files changed

+12
-2
lines changed
Filter options

3 files changed

+12
-2
lines changed

‎src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+1-1Lines 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
}

‎src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php
+8Lines 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
}

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+3-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.