Skip to content

Commit 206351d

Browse files
bug #62325 [Routing] Fix default value not taken if usigng name:entity.attribute (eltharin)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Routing] Fix default value not taken if usigng name:entity.attribute | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT When using Mapped parameters, default values can't be used : ```php #[Route('/zoom/{vortexLibelle:vortex.libelle}/{libelle:otherVortex}', name: 'zoom', methods: ['GET'])] #[AjaxCallOrNot] public function zoom(VortexManager $vr, Vortex $vortex, ?Vortex $otherVortex = null): Response { ... } ``` or ```php #[Route('/zoom/{vortexLibelle:vortex.libelle}/{otherVortexLibelle:otherVortex.libelle}', name: 'zoom', methods: ['GET'])] #[AjaxCallOrNot] public function zoom(VortexManager $vr, Vortex $vortex, ?Vortex $otherVortex = null): Response { ... } ``` route was not found if we want to go to : /zoom/myVortex, with this fix, it's OK Commits ------- f7c898f [Routing] Fix default value not taken if usigng name:entity.attribute
2 parents 52deec5 + f7c898f commit 206351d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/Symfony/Component/Routing/Loader/AttributeClassLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ protected function addRoute(RouteCollection $collection, object $attr, array $gl
219219
continue;
220220
}
221221
foreach ($paths as $locale => $path) {
222-
if (preg_match(\sprintf('/\{%s(?:<.*?>)?\}/', preg_quote($param->name)), $path)) {
222+
if (preg_match(\sprintf('/\{(?|([^\}:<]++):%s(?:\.[^\}<]++)?|(%1$s))(?:<.*?>)?\}/', preg_quote($param->name)), $path, $matches)) {
223223
if (\is_scalar($defaultValue = $param->getDefaultValue()) || null === $defaultValue) {
224-
$defaults[$param->name] = $defaultValue;
224+
$defaults[$matches[1]] = $defaultValue;
225225
} elseif ($defaultValue instanceof \BackedEnum) {
226-
$defaults[$param->name] = $defaultValue->value;
226+
$defaults[$matches[1]] = $defaultValue->value;
227227
}
228228
break;
229229
}

src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/DefaultValueController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
44

55
use Symfony\Component\Routing\Attribute\Route;
6+
use Symfony\Component\Routing\Tests\Fixtures\AttributedClasses\BarClass;
67
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestIntBackedEnum;
78
use Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum;
89

@@ -30,4 +31,14 @@ public function stringEnumAction(TestStringBackedEnum $default = TestStringBacke
3031
public function intEnumAction(TestIntBackedEnum $default = TestIntBackedEnum::Diamonds)
3132
{
3233
}
34+
35+
#[Route(path: '/defaultMappedParam/{libelle:bar}', name: 'defaultMappedParam_default')]
36+
public function defaultMappedParam(?BarClass $bar = null)
37+
{
38+
}
39+
40+
#[Route(path: '/defaultAdvancedMappedParam/{barLibelle:bar.libelle}', name: 'defaultAdvancedMappedParam_default')]
41+
public function defaultAdvancedMappedParam(?BarClass $bar = null)
42+
{
43+
}
3344
}

src/Symfony/Component/Routing/Tests/Loader/AttributeClassLoaderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,16 @@ public function testLocalizedPathRoutesWithExplicitPathPropety()
171171
public function testDefaultValuesForMethods()
172172
{
173173
$routes = $this->loader->load(DefaultValueController::class);
174-
$this->assertCount(5, $routes);
174+
$this->assertCount(7, $routes);
175175
$this->assertEquals('/{default}/path', $routes->get('action')->getPath());
176176
$this->assertEquals('value', $routes->get('action')->getDefault('default'));
177177
$this->assertEquals('Symfony', $routes->get('hello_with_default')->getDefault('name'));
178178
$this->assertEquals('World', $routes->get('hello_without_default')->getDefault('name'));
179179
$this->assertEquals('diamonds', $routes->get('string_enum_action')->getDefault('default'));
180+
$this->assertArrayHasKey('libelle', $routes->get('defaultMappedParam_default')->getDefaults());
181+
$this->assertNull($routes->get('defaultMappedParam_default')->getDefault('libelle'));
182+
$this->assertArrayHasKey('barLibelle', $routes->get('defaultAdvancedMappedParam_default')->getDefaults());
183+
$this->assertNull($routes->get('defaultAdvancedMappedParam_default')->getDefault('barLibelle'));
180184
$this->assertEquals(20, $routes->get('int_enum_action')->getDefault('default'));
181185
}
182186

0 commit comments

Comments
 (0)