Skip to content

[Routing] Fix conflicting FQCN aliases with route name #52805

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
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
14 changes: 11 additions & 3 deletions src/Symfony/Component/Routing/Loader/AttributeClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ public function load(mixed $class, string $type = null): RouteCollection

if (1 === $collection->count() - \count($routeNamesBefore)) {
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
$collection->addAlias(sprintf('%s::%s', $class->name, $method->name), $newRouteName);
if ($newRouteName !== $aliasName = sprintf('%s::%s', $class->name, $method->name)) {
$collection->addAlias($aliasName, $newRouteName);
}
}
}
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
Expand All @@ -155,8 +157,14 @@ public function load(mixed $class, string $type = null): RouteCollection
}
}
if ($fqcnAlias && 1 === $collection->count()) {
$collection->addAlias($class->name, $invokeRouteName = key($collection->all()));
$collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName);
$invokeRouteName = key($collection->all());
if ($invokeRouteName !== $class->name) {
$collection->addAlias($class->name, $invokeRouteName);
}

if ($invokeRouteName !== $aliasName = sprintf('%s::__invoke', $class->name)) {
$collection->addAlias($aliasName, $invokeRouteName);
}
}

if ($this->hasDeprecatedAnnotations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;

use Symfony\Component\Routing\Attribute\Route;

/**
* @Route("/foobarccc", name="Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableFQCNAliasConflictController")
*/
class InvokableFQCNAliasConflictController
{
public function __invoke()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;

use Symfony\Component\Routing\Attribute\Route;

#[Route(path: '/foobarccc', name: self::class)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added only this test fixture case but the 3 FQCN aliases are susceptible to fail.

class InvokableFQCNAliasConflictController
{
public function __invoke()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public function testInvokableControllerLoader()
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController::__invoke'));
}

public function testInvokableFQCNAliasConflictController()
{
$routes = $this->loader->load($this->getNamespace().'\InvokableFQCNAliasConflictController');
$this->assertCount(1, $routes);
$this->assertEquals('/foobarccc', $routes->get($this->getNamespace().'\InvokableFQCNAliasConflictController')->getPath());
$this->assertNull($routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController'));
$this->assertEquals(new Alias($this->getNamespace().'\InvokableFQCNAliasConflictController'), $routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController::__invoke'));
}

public function testInvokableMethodControllerLoader()
{
$routes = $this->loader->load($this->getNamespace().'\InvokableMethodController');
Expand Down