Skip to content
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
2 changes: 1 addition & 1 deletion src/Symfony/Routing/IriConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
if (\is_string($expectedClass) && $expectedClass !== $parameters['_api_resource_class'] && class_exists($expectedClass)) {
foreach ($this->resourceMetadataCollectionFactory->create($expectedClass) as $resourceMetadata) {
foreach ($resourceMetadata->getOperations() ?? [] as $candidate) {
if ($candidate instanceof HttpOperation && !$candidate instanceof CollectionOperationInterface && $candidate->getRouteName() === ($parameters['_route'] ?? null)) {
if ($candidate instanceof HttpOperation && !$candidate instanceof CollectionOperationInterface && $candidate->getRouteName() === ($parameters['_route'] ?? null) && $candidate->getUriTemplate() === $routeOperation->getUriTemplate()) {
$dispatchOperation = $candidate;
break 2;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/SharedRouteAlpha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\Get;

#[Get(name: 'shared_route_alpha', routeName: 'shared_static_route', uriVariables: [])]
final class SharedRouteAlpha
{
}
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/SharedRouteBeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\Get;

#[Get(name: 'shared_route_beta', routeName: 'shared_static_route', uriVariables: [])]
final class SharedRouteBeta
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[Get(uriTemplate: '/shared_route_variable/alpha/{id}', name: 'shared_route_variable_alpha_get', routeName: 'shared_route_variable_item', openapi: false, provider: [self::class, 'provide'])]
final class VariableRouteAlpha
{
public function __construct(public int $id = 0)
{
}

public static function provide(Operation $operation, array $uriVariables = []): self
{
return new self((int) $uriVariables['id']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[Get(uriTemplate: '/shared_route_variable/beta/{id}', name: 'shared_route_variable_beta_get', routeName: 'shared_route_variable_item', openapi: false, provider: [self::class, 'provide'])]
final class VariableRouteBeta
{
public function __construct(public int $id = 0)
{
}

public static function provide(Operation $operation, array $uriVariables = []): self
{
return new self((int) $uriVariables['id']);
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/app/config/routing_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ relation_embedded.custom_get:
methods: ['GET', 'HEAD']
defaults:
_controller: 'ApiPlatform\Tests\Fixtures\TestBundle\Controller\CustomController::customAction'

shared_static_route:
path: '/shared_static_route'
methods: ['GET', 'HEAD']
defaults:
_controller: 'api_platform.action.placeholder'

shared_route_variable_item:
path: '/shared_route_variable/{kind}/{id}'
methods: ['GET', 'HEAD']
defaults:
_controller: 'api_platform.action.placeholder'
_api_resource_class: 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\VariableRouteAlpha'
_api_operation_name: 'shared_route_variable_alpha_get'
13 changes: 12 additions & 1 deletion tests/Functional/Serializer/SharedRouteIriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace ApiPlatform\Tests\Functional\Serializer;

use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\CategoryProjection;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\CategoryResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\TopicResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\VariableRouteAlpha;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteIri\VariableRouteBeta;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class SharedRouteIriTest extends ApiTestCase
Expand All @@ -27,7 +30,7 @@ final class SharedRouteIriTest extends ApiTestCase

public static function getResources(): array
{
return [CategoryResource::class, CategoryProjection::class, TopicResource::class];
return [CategoryResource::class, CategoryProjection::class, TopicResource::class, VariableRouteAlpha::class, VariableRouteBeta::class];
}

public function testBorrowedRouteIriCanBeWrittenBackToTheSameRelation(): void
Expand Down Expand Up @@ -60,4 +63,12 @@ public function testUnrelatedResourceIriIsRejected(): void
self::assertResponseStatusCodeSame(400);
self::assertJsonContains(['detail' => 'Invalid IRI "/shared_iri/topics/1".']);
}

public function testRouteSharedAcrossUriTemplatesIsNotBorrowedByTheExpectedClass(): void
{
$iriConverter = static::getContainer()->get('api_platform.iri_converter');
\assert($iriConverter instanceof IriConverterInterface);

$this->assertInstanceOf(VariableRouteAlpha::class, $iriConverter->getResourceFromIri('/shared_route_variable/alpha/1', ['resource_class' => VariableRouteBeta::class]));
}
}
53 changes: 53 additions & 0 deletions tests/Functional/SharedRouteNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional;

use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteAlpha;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SharedRouteBeta;
use ApiPlatform\Tests\SetupClassResourcesTrait;

final class SharedRouteNameTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

public static function getResources(): array
{
return [SharedRouteAlpha::class, SharedRouteBeta::class];
}

public function testOperationsSharingARouteNameKeepDistinctNames(): void
{
$container = static::getContainer();
$resourceMetadataCollectionFactory = $container->get('api_platform.metadata.resource.metadata_collection_factory');
\assert($resourceMetadataCollectionFactory instanceof ResourceMetadataCollectionFactoryInterface);
$iriConverter = $container->get('api_platform.iri_converter');
\assert($iriConverter instanceof IriConverterInterface);

$alpha = $resourceMetadataCollectionFactory->create(SharedRouteAlpha::class)->getOperation('shared_route_alpha');
$beta = $resourceMetadataCollectionFactory->create(SharedRouteBeta::class)->getOperation('shared_route_beta');

$this->assertInstanceOf(HttpOperation::class, $alpha);
$this->assertInstanceOf(HttpOperation::class, $beta);
$this->assertSame('shared_static_route', $alpha->getRouteName());
$this->assertSame('shared_static_route', $beta->getRouteName());
$this->assertSame('/shared_static_route', $iriConverter->getIriFromResource(new SharedRouteAlpha(), operation: $alpha));
$this->assertSame('/shared_static_route', $iriConverter->getIriFromResource(new SharedRouteBeta(), operation: $beta));
}
}
Loading