From 9b37793cbec60a8919db1fba38b11b3dc9aebaec Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Mon, 1 Apr 2019 19:19:31 +0300 Subject: [PATCH] [Routing] Fix: annotation loader ignores method's default values --- .../Component/Routing/Loader/AnnotationClassLoader.php | 2 +- .../AnnotationFixtures/DefaultValueController.php | 8 ++++++++ .../Routing/Tests/Loader/AnnotationClassLoaderTest.php | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index 0f5dcdb82aa43..ae834bf4ba5c4 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -196,7 +196,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl continue; } foreach ($paths as $locale => $path) { - if (false !== strpos($path, sprintf('{%s}', $param->name))) { + if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) { $defaults[$param->name] = $param->getDefaultValue(); break; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php index e66bee91831a4..f7e38c290d2e7 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php @@ -12,4 +12,12 @@ class DefaultValueController public function action($default = 'value') { } + + /** + * @Route("/hello/{name<\w+>}", name="hello_without_default") + * @Route("/hello/{name<\w+>?Symfony}", name="hello_with_default") + */ + public function hello(string $name = 'World') + { + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php index 74dfcf8f49ff8..491c2b60071b5 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php @@ -136,9 +136,11 @@ public function testLocalizedPathRoutesWithExplicitPathPropety() public function testDefaultValuesForMethods() { $routes = $this->loader->load(DefaultValueController::class); - $this->assertCount(1, $routes); + $this->assertCount(3, $routes); $this->assertEquals('/{default}/path', $routes->get('action')->getPath()); $this->assertEquals('value', $routes->get('action')->getDefault('default')); + $this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name')); + $this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name')); } public function testMethodActionControllers() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy