Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Convert BackedEnums passed as controller action parameters to their value #50031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down