diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index 6da68c7594544..b06c649885b56 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -208,7 +208,11 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g } foreach ($paths as $locale => $path) { if (preg_match(sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) { - $defaults[$param->name] = $param->getDefaultValue(); + if (\is_scalar($defaultValue = $param->getDefaultValue()) || null === $defaultValue) { + $defaults[$param->name] = $defaultValue; + } elseif ($defaultValue instanceof \BackedEnum) { + $defaults[$param->name] = $defaultValue->value; + } break; } } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php index f7e38c290d2e7..c5e0c20d356e1 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/DefaultValueController.php @@ -3,6 +3,8 @@ namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum; +use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum; class DefaultValueController { @@ -20,4 +22,18 @@ public function action($default = 'value') public function hello(string $name = 'World') { } + + /** + * @Route("/enum/{default}", name="string_enum_action") + */ + public function stringEnumAction(TestStringBackedEnum $default = TestStringBackedEnum::Diamonds) + { + } + + /** + * @Route("/enum/{default<\d+>}", name="int_enum_action") + */ + public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds) + { + } } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php index 5bbfb0126dd2b..4d0df50698c22 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php @@ -3,6 +3,8 @@ namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum; +use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum; class DefaultValueController { @@ -18,4 +20,14 @@ public function action($default = 'value') public function hello(string $name = 'World') { } + + #[Route(path: '/enum/{default}', name: 'string_enum_action')] + public function stringEnumAction(TestStringBackedEnum $default = TestStringBackedEnum::Diamonds) + { + } + + #[Route(path: '/enum/{default<\d+>}', name: 'int_enum_action')] + public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds) + { + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTestCase.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTestCase.php index b717d19609794..62a1d423441eb 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTestCase.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTestCase.php @@ -104,11 +104,13 @@ public function testLocalizedPathRoutesWithExplicitPathPropety() public function testDefaultValuesForMethods() { $routes = $this->loader->load($this->getNamespace().'\DefaultValueController'); - $this->assertCount(3, $routes); + $this->assertCount(5, $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')); + $this->assertEquals('diamonds', $routes->get('string_enum_action')->getDefault('default')); + $this->assertEquals(20, $routes->get('int_enum_action')->getDefault('default')); } public function testMethodActionControllers()