Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Routing] Fix default value not taken if usigng name:entity.attribute
  • Loading branch information
eltharin authored and nicolas-grekas committed Nov 12, 2025
commit f7c898fe0c41757ecd00b912e181e8e28c9bccfe
6 changes: 3 additions & 3 deletions src/Symfony/Component/Routing/Loader/AttributeClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down