Skip to content

[Routing] Router ignores inline requirements of important param #40749

Closed
@Foxprodev

Description

@Foxprodev

Symfony version(s) affected: 4.4 5.2?

Description
As I mentioned here #40701 (comment). The problem not only in UrlGenerator. Router does not check important (I mean started with !) params inline requirements at all. I decided to open separate issue, because it could be more dangerous and it can be fixed separately.

How to reproduce
Create route with important param, and set some inline requirements like:
#[Route('/foo/{!id<\d+>}')]
Open /foo/bar url and yes route matches the requirement

Reason

preg_match_all('#\{(!)?(\w+)\}#', $pattern, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER);
foreach ($matches as $match) {
$important = $match[1][1] >= 0;
$varName = $match[2][0];

$varName does not start with !
$regexp = $route->getRequirement($varName);

However Route requirement key starts with !, and param requirement set do default regex like [^/]++

Possible Solution

private function extractInlineDefaultsAndRequirements(string $pattern): string
{
if (false === strpbrk($pattern, '?<')) {
return $pattern;
}
return preg_replace_callback('#\{(!?\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
if (isset($m[3][0])) {
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
}
if (isset($m[2][0])) {
$this->setRequirement($m[1], substr($m[2], 1, -1));
}
return '{'.$m[1].'}';
}, $pattern);
}

Do not include ! in requirement key.
However it does not fix defaults for UrlGenerator described here #40701

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions