Skip to content

Commit

Permalink
minor #50107 [Routing] Prevent duplicated methods and schemes in Rout…
Browse files Browse the repository at this point in the history
…e (fancyweb)

This PR was merged into the 6.4 branch.

Discussion
----------

[Routing] Prevent duplicated methods and schemes in Route

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | -
| Tickets       | -
| License       | MIT
| Doc PR        | -

Methods and schemes can be "duplicated" if declared globally on the controller and also on the method. It impacts the `debug:router` command displaying `GET|GET` for the methods column for example.

Commits
-------

5923d07 [Routing] Prevent duplicated methods and schemes in Route
  • Loading branch information
nicolas-grekas committed Jun 29, 2023
2 parents 261a8ed + 5923d07 commit 48e1444
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Expand Up @@ -183,8 +183,8 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
$requirements = array_replace($globals['requirements'], $requirements);
$options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_merge($globals['schemes'], $annot->getSchemes());
$methods = array_merge($globals['methods'], $annot->getMethods());
$schemes = array_unique(array_merge($globals['schemes'], $annot->getSchemes()));
$methods = array_unique(array_merge($globals['methods'], $annot->getMethods()));

$host = $annot->getHost() ?? $globals['host'];
$condition = $annot->getCondition() ?? $globals['condition'];
Expand Down
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/defaults", locale="g_locale", format="g_format")
* @Route("/defaults", methods="GET", schemes="https", locale="g_locale", format="g_format")
*/
class GlobalDefaultsClass
{
Expand All @@ -31,4 +31,18 @@ public function locale()
public function format()
{
}

/**
* @Route("/redundant-method", name="redundant_method", methods="GET")
*/
public function redundantMethod()
{
}

/**
* @Route("/redundant-scheme", name="redundant_scheme", methods="https")
*/
public function redundantScheme()
{
}
}
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/defaults', locale: 'g_locale', format: 'g_format')]
#[Route(path: '/defaults', methods: ['GET'], schemes: ['https'], locale: 'g_locale', format: 'g_format')]
class GlobalDefaultsClass
{
#[Route(path: '/specific-locale', name: 'specific_locale', locale: 's_locale')]
Expand All @@ -25,4 +25,14 @@ public function locale()
public function format()
{
}

#[Route(path: '/redundant-method', name: 'redundant_method', methods: ['GET'])]
public function redundantMethod()
{
}

#[Route(path: '/redundant-scheme', name: 'redundant_scheme', schemes: ['https'])]
public function redundantScheme()
{
}
}
Expand Up @@ -149,7 +149,7 @@ public function testInvokableClassRouteLoadWithMethodAnnotation()
public function testGlobalDefaultsRoutesLoadWithAnnotation()
{
$routes = $this->loader->load($this->getNamespace().'\GlobalDefaultsClass');
$this->assertCount(2, $routes);
$this->assertCount(4, $routes);

$specificLocaleRoute = $routes->get('specific_locale');

Expand All @@ -162,6 +162,9 @@ public function testGlobalDefaultsRoutesLoadWithAnnotation()
$this->assertSame('/defaults/specific-format', $specificFormatRoute->getPath());
$this->assertSame('g_locale', $specificFormatRoute->getDefault('_locale'));
$this->assertSame('s_format', $specificFormatRoute->getDefault('_format'));

$this->assertSame(['GET'], $routes->get('redundant_method')->getMethods());
$this->assertSame(['https'], $routes->get('redundant_scheme')->getSchemes());
}

public function testUtf8RoutesLoadWithAnnotation()
Expand Down

0 comments on commit 48e1444

Please sign in to comment.