Description
Symfony version(s) affected: 5.2.3 (previous versions are probably affected too)
Description/How to reproduce
Using the syntax @Route("sitemap/pages.{!_format<xml>?xml}", name="sitemap__pages")
as documented here doesn't register the default value properly.
Generating the URL like this $this->generateUrl('sitemap__pages')
throws a MissingMandatoryParametersException
for _format
.
Changing the route definition to @Route("sitemap/pages.{!_format<xml>?xml}", name="sitemap__pages", defaults={"_format"="xml"})
works as expected, but this way I'm specifying the default value twice...
Possible Solution
The exception is thrown by https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L178. I dumped $defaults
passed to doGenerate
and I found that it contains:
array:2 [▼
"!_format" => "xml"
"_controller" => "App\Controller\FrontEnd\SitemapController::pages"
]
The default value for _format
is not found because of the leading !
in the key.
Inline defaults and requirements are extracted: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Route.php#L536
I'm afraid that 826db22 (by @nicolas-grekas) is not compatible with this check: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L177
Unfortunately I can't open a PR because I'm not sure if I should update the way defaults/requirements/params are stored inside the Route object or if I should update the check inside UrlGenerator
.
Thank you for your time.