Verified Commit 97a0dbb3 authored by Dave Long's avatar Dave Long
Browse files

fix: #3600697 Attribute route discovery does not handle arrays for JSON:API contrib modules

By: ptmkenny
By: kieran.cott
By: guillaumeg
parent 4cc588f7
Loading
Loading
Loading
Loading
Loading
+26 −24
Original line number Diff line number Diff line
@@ -19,9 +19,9 @@
class AttributeRouteDiscovery extends StaticRouteDiscoveryBase {

  /**
   * @param \Traversable<string, string> $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Traversable<string, string|array<int, string>> $namespaces
   *   An object implementing \Traversable containing root paths keyed by the
   *   corresponding namespace to look for controller implementations.
   */
  public function __construct(
    protected readonly \Traversable $namespaces,
@@ -43,7 +43,8 @@ protected function collectRoutes(): iterable {
      'Form' => $this->createFormRouteCollection(...),
    ];

    foreach ($this->namespaces as $namespace => $directory) {
    foreach ($this->namespaces as $namespace => $directories) {
      foreach ((array) $directories as $directory) {
        foreach ($routeTypes as $routeType => $factory) {
          $routeDirectory = $directory . '/' . $routeType;
          $routeNamespace = $namespace . '\\' . $routeType;
@@ -71,6 +72,7 @@ protected function collectRoutes(): iterable {
        }
      }
    }
  }

  /**
   * Creates a route collection from a controller class's attributed methods.
+20 −0
Original line number Diff line number Diff line
@@ -45,6 +45,26 @@ protected function setUp(): void {
    $this->routeCollection = $event->getRouteCollection();
  }

  /**
   * @legacy-covers ::onRouteBuild
   */
  public function testOnRouteBuildWithArrayNamespaceDirectories(): void {
    $event = new RouteBuildEvent(new RouteCollection());
    $namespaces = new \ArrayObject([
      'Drupal\router_test' => [
        $this->root . '/core/modules/system/tests/modules/router_test_directory/missing',
        $this->root . '/core/modules/system/tests/modules/router_test_directory/src',
      ],
    ]);
    $discovery = new AttributeRouteDiscovery($namespaces);
    $discovery->onRouteBuild($event);

    $route = $event->getRouteCollection()->get('router_test.method_attribute');
    $this->assertNotNull($route);
    $this->assertSame('/test_method_attribute', $route->getPath());
    $this->assertSame(TestAttributes::class . '::attributeMethod', $route->getDefault('_controller'));
  }

  /**
   * @legacy-covers ::onRouteBuild
   */