Skip to content

Commit 0019813

Browse files
committed
fix(metadata): do not override name fixes api-platform#5235
1 parent 40b637f commit 0019813

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

src/Metadata/Resource/Factory/OperationDefaultsTrait.php

-11
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,6 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
187187
$operation = $operation->withName($operation->getRouteName());
188188
}
189189

190-
// Check for name conflict
191-
if ($operation->getName() && null !== ($operations = $resource->getOperations())) {
192-
if (!$operations->has($operation->getName())) {
193-
return [$operation->getName(), $operation];
194-
}
195-
196-
$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()));
197-
/** @var HttpOperation $operation */
198-
$operation = $operation->withName('');
199-
}
200-
201190
$operationName = $operation->getName() ?? sprintf(
202191
'_api_%s_%s%s',
203192
$operation->getUriTemplate() ?: $operation->getShortName(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
4+
5+
use ApiPlatform\Metadata\ApiProperty;
6+
use ApiPlatform\Metadata\ApiResource;
7+
use ApiPlatform\Metadata\Get;
8+
use ApiPlatform\Metadata\Post;
9+
10+
#[
11+
ApiResource(
12+
shortName: 'User',
13+
operations: [
14+
new Post(
15+
uriTemplate: '/password/reset',
16+
name: 'password_reset',
17+
),
18+
new Post(
19+
uriTemplate: '/password/set',
20+
name: 'password_set',
21+
),
22+
new Get(
23+
uriTemplate: '/password/reset/{token}',
24+
name: 'password_reset_token',
25+
),
26+
],
27+
routePrefix: '/users'
28+
)
29+
]
30+
/**
31+
* Test for issue #5235
32+
*/
33+
class PasswordResource
34+
{
35+
#[ApiProperty(identifier: true)]
36+
public ?string $token = null;
37+
}

tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use ApiPlatform\Metadata\Put;
2828
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
2929
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PasswordResource;
3031
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations;
3132
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation;
3233
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource;
@@ -226,4 +227,19 @@ class: AttributeOnlyOperation::class,
226227
),
227228
]), $attributeResourceMetadataCollectionFactory->create(AttributeOnlyOperation::class));
228229
}
230+
231+
/**
232+
* Tests issue #5235
233+
*/
234+
public function testNameDeclarationShouldNotBeRemoved(): void
235+
{
236+
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory();
237+
238+
$operation = new HttpOperation(shortName: 'AttributeOnlyOperation', class: AttributeOnlyOperation::class);
239+
$metadataCollection = $attributeResourceMetadataCollectionFactory->create(PasswordResource::class);
240+
$operations = $metadataCollection[0]->getOperations();
241+
$this->assertTrue($operations->has('password_set'));
242+
$this->assertTrue($operations->has('password_reset_token'));
243+
$this->assertTrue($operations->has('password_reset'));
244+
}
229245
}

0 commit comments

Comments
 (0)