Skip to content

Commit 80222b0

Browse files
committed
[Routing] Prevent duplicated methods and schemes in Route
1 parent d02d2c4 commit 80222b0

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Loader/AnnotationClassLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g
183183
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
184184
$requirements = array_replace($globals['requirements'], $requirements);
185185
$options = array_replace($globals['options'], $annot->getOptions());
186-
$schemes = array_merge($globals['schemes'], $annot->getSchemes());
187-
$methods = array_merge($globals['methods'], $annot->getMethods());
186+
$schemes = array_unique(array_merge($globals['schemes'], $annot->getSchemes()));
187+
$methods = array_unique(array_merge($globals['methods'], $annot->getMethods()));
188188

189189
$host = $annot->getHost() ?? $globals['host'];
190190
$condition = $annot->getCondition() ?? $globals['condition'];

Tests/Fixtures/AnnotationFixtures/GlobalDefaultsClass.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Routing\Annotation\Route;
1515

1616
/**
17-
* @Route("/defaults", locale="g_locale", format="g_format")
17+
* @Route("/defaults", methods="GET", schemes="https", locale="g_locale", format="g_format")
1818
*/
1919
class GlobalDefaultsClass
2020
{
@@ -31,4 +31,18 @@ public function locale()
3131
public function format()
3232
{
3333
}
34+
35+
/**
36+
* @Route("/redundant-method", name="redundant_method", methods="GET")
37+
*/
38+
public function redundantMethod()
39+
{
40+
}
41+
42+
/**
43+
* @Route("/redundant-scheme", name="redundant_scheme", methods="https")
44+
*/
45+
public function redundantScheme()
46+
{
47+
}
3448
}

Tests/Fixtures/AttributeFixtures/GlobalDefaultsClass.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\Routing\Annotation\Route;
1515

16-
#[Route(path: '/defaults', locale: 'g_locale', format: 'g_format')]
16+
#[Route(path: '/defaults', methods: ['GET'], schemes: ['https'], locale: 'g_locale', format: 'g_format')]
1717
class GlobalDefaultsClass
1818
{
1919
#[Route(path: '/specific-locale', name: 'specific_locale', locale: 's_locale')]
@@ -25,4 +25,14 @@ public function locale()
2525
public function format()
2626
{
2727
}
28+
29+
#[Route(path: '/redundant-method', name: 'redundant_method', methods: ['GET'])]
30+
public function redundantMethod()
31+
{
32+
}
33+
34+
#[Route(path: '/redundant-scheme', name: 'redundant_scheme', schemes: ['https'])]
35+
public function redundantScheme()
36+
{
37+
}
2838
}

Tests/Loader/AnnotationClassLoaderTestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testInvokableClassRouteLoadWithMethodAnnotation()
149149
public function testGlobalDefaultsRoutesLoadWithAnnotation()
150150
{
151151
$routes = $this->loader->load($this->getNamespace().'\GlobalDefaultsClass');
152-
$this->assertCount(2, $routes);
152+
$this->assertCount(4, $routes);
153153

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

@@ -162,6 +162,9 @@ public function testGlobalDefaultsRoutesLoadWithAnnotation()
162162
$this->assertSame('/defaults/specific-format', $specificFormatRoute->getPath());
163163
$this->assertSame('g_locale', $specificFormatRoute->getDefault('_locale'));
164164
$this->assertSame('s_format', $specificFormatRoute->getDefault('_format'));
165+
166+
$this->assertSame(['GET'], $routes->get('redundant_method')->getMethods());
167+
$this->assertSame(['https'], $routes->get('redundant_scheme')->getSchemes());
165168
}
166169

167170
public function testUtf8RoutesLoadWithAnnotation()

0 commit comments

Comments
 (0)