Skip to content

Commit 3eb32c5

Browse files
committed
fix(metadata): do not override name fixes api-platform#5235
1 parent 08e532c commit 3eb32c5

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-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,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Get;
19+
use ApiPlatform\Metadata\Post;
20+
21+
#[
22+
ApiResource(
23+
shortName: 'PasswordResource',
24+
operations: [
25+
new Post(
26+
uriTemplate: '/password/reset',
27+
name: 'password_reset',
28+
),
29+
new Post(
30+
uriTemplate: '/password/set',
31+
name: 'password_set',
32+
),
33+
new Get(
34+
uriTemplate: '/password/reset/{token}',
35+
name: 'password_reset_token',
36+
),
37+
],
38+
routePrefix: '/users'
39+
)
40+
]
41+
/**
42+
* Test for issue #5235.
43+
*/
44+
class PasswordResource
45+
{
46+
#[ApiProperty(identifier: true)]
47+
public ?string $token = null;
48+
}

tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

+15
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,18 @@ 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+
$metadataCollection = $attributeResourceMetadataCollectionFactory->create(PasswordResource::class);
239+
$operations = $metadataCollection[0]->getOperations();
240+
$this->assertTrue($operations->has('password_set'));
241+
$this->assertTrue($operations->has('password_reset_token'));
242+
$this->assertTrue($operations->has('password_reset'));
243+
}
229244
}

0 commit comments

Comments
 (0)