From 3eb32c5ff703cbb44f8aa899004561e491428acb Mon Sep 17 00:00:00 2001 From: soyuka Date: Mon, 28 Nov 2022 13:30:28 +0100 Subject: [PATCH] fix(metadata): do not override name fixes #5235 --- .../Factory/OperationDefaultsTrait.php | 11 ----- .../ApiResource/PasswordResource.php | 48 +++++++++++++++++++ ...sResourceMetadataCollectionFactoryTest.php | 15 ++++++ 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 tests/Fixtures/TestBundle/ApiResource/PasswordResource.php diff --git a/src/Metadata/Resource/Factory/OperationDefaultsTrait.php b/src/Metadata/Resource/Factory/OperationDefaultsTrait.php index 93e6c83857c..d320bd41577 100644 --- a/src/Metadata/Resource/Factory/OperationDefaultsTrait.php +++ b/src/Metadata/Resource/Factory/OperationDefaultsTrait.php @@ -187,17 +187,6 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper $operation = $operation->withName($operation->getRouteName()); } - // Check for name conflict - if ($operation->getName() && null !== ($operations = $resource->getOperations())) { - if (!$operations->has($operation->getName())) { - return [$operation->getName(), $operation]; - } - - $this->logger->warning(sprintf('The operation "%s" already exists on the resource "%s", pick a different name or leave it empty. In the meantime we will generate a unique name.', $operation->getName(), $resource->getClass())); - /** @var HttpOperation $operation */ - $operation = $operation->withName(''); - } - $operationName = $operation->getName() ?? sprintf( '_api_%s_%s%s', $operation->getUriTemplate() ?: $operation->getShortName(), diff --git a/tests/Fixtures/TestBundle/ApiResource/PasswordResource.php b/tests/Fixtures/TestBundle/ApiResource/PasswordResource.php new file mode 100644 index 00000000000..458ccc3977d --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/PasswordResource.php @@ -0,0 +1,48 @@ + + * + * 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\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\Post; + +#[ + ApiResource( + shortName: 'PasswordResource', + operations: [ + new Post( + uriTemplate: '/password/reset', + name: 'password_reset', + ), + new Post( + uriTemplate: '/password/set', + name: 'password_set', + ), + new Get( + uriTemplate: '/password/reset/{token}', + name: 'password_reset_token', + ), + ], + routePrefix: '/users' + ) +] +/** + * Test for issue #5235. + */ +class PasswordResource +{ + #[ApiProperty(identifier: true)] + public ?string $token = null; +} diff --git a/tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php b/tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php index 6510080c88d..67aa6bbe198 100644 --- a/tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php +++ b/tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php @@ -27,6 +27,7 @@ use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory; use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PasswordResource; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource; @@ -226,4 +227,18 @@ class: AttributeOnlyOperation::class, ), ]), $attributeResourceMetadataCollectionFactory->create(AttributeOnlyOperation::class)); } + + /** + * Tests issue #5235. + */ + public function testNameDeclarationShouldNotBeRemoved(): void + { + $attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory(); + + $metadataCollection = $attributeResourceMetadataCollectionFactory->create(PasswordResource::class); + $operations = $metadataCollection[0]->getOperations(); + $this->assertTrue($operations->has('password_set')); + $this->assertTrue($operations->has('password_reset_token')); + $this->assertTrue($operations->has('password_reset')); + } }