diff --git a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php index b73e94ea09819..f2bc668d09cb1 100644 --- a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php @@ -219,11 +219,11 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl continue; } foreach ($paths as $locale => $path) { - if (preg_match(\sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) { + if (preg_match(\sprintf('/\{(?|([^\}:<]++):%s(?:\.[^\}<]++)?|(%1$s))(?:<.*?>)?\}/', preg_quote($param->name)), $path, $matches)) { if (\is_scalar($defaultValue = $param->getDefaultValue()) || null === $defaultValue) { - $defaults[$param->name] = $defaultValue; + $defaults[$matches[1]] = $defaultValue; } elseif ($defaultValue instanceof \BackedEnum) { - $defaults[$param->name] = $defaultValue->value; + $defaults[$matches[1]] = $defaultValue->value; } break; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php index dc5d0c4e52ee3..5dcf5181c0e01 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php @@ -3,6 +3,7 @@ namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Component\Routing\Tests\Fixtures\AttributedClasses\BarClass; use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum; use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum; @@ -30,4 +31,14 @@ public function stringEnumAction(TestStringBackedEnum $default = TestStringBacke public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds) { } + + #[Route(path: '/defaultMappedParam/{libelle:bar}', name: 'defaultMappedParam_default')] + public function defaultMappedParam(?BarClass $bar = null) + { + } + + #[Route(path: '/defaultAdvancedMappedParam/{barLibelle:bar.libelle}', name: 'defaultAdvancedMappedParam_default')] + public function defaultAdvancedMappedParam(?BarClass $bar = null) + { + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php index 50a10a16cac2f..022e0c9f83ea5 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php @@ -171,12 +171,16 @@ public function testLocalizedPathRoutesWithExplicitPathPropety() public function testDefaultValuesForMethods() { $routes = $this->loader->load(DefaultValueController::class); - $this->assertCount(5, $routes); + $this->assertCount(7, $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->assertArrayHasKey('libelle', $routes->get('defaultMappedParam_default')->getDefaults()); + $this->assertNull($routes->get('defaultMappedParam_default')->getDefault('libelle')); + $this->assertArrayHasKey('barLibelle', $routes->get('defaultAdvancedMappedParam_default')->getDefaults()); + $this->assertNull($routes->get('defaultAdvancedMappedParam_default')->getDefault('barLibelle')); $this->assertEquals(20, $routes->get('int_enum_action')->getDefault('default')); }