Skip to content

Commit 8250d41

Browse files
authored
fix(metadata): define a name on a single operation (#5090)
fixes #5082
1 parent 310363d commit 8250d41

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
215215
}
216216

217217
// Check for name conflict
218-
if ($operation->getName()) {
219-
if (null !== $resource->getOperations() && !$resource->getOperations()->has($operation->getName())) {
218+
if ($operation->getName() && null !== ($operations = $resource->getOperations())) {
219+
if (!$operations->has($operation->getName())) {
220220
return [$operation->getName(), $operation];
221221
}
222222

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Entity;
15+
16+
use ApiPlatform\Metadata\Get;
17+
18+
#[Get(name: 'my own name')]
19+
final class AttributeOnlyOperation
20+
{
21+
}

tests/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
3030
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
3131
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations;
32+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation;
3233
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource;
3334
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResources;
3435
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ExtraPropertiesResource;
@@ -206,4 +207,20 @@ public function testExtraProperties(): void
206207
$this->assertEquals($extraPropertiesResource[0]->getExtraProperties(), ['foo' => 'bar']);
207208
$this->assertEquals($extraPropertiesResource->getOperation('_api_ExtraPropertiesResource_get')->getExtraProperties(), ['foo' => 'bar']);
208209
}
210+
211+
public function testOverrideNameWithoutOperations(): void
212+
{
213+
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory();
214+
215+
$operation = new HttpOperation(shortName: 'AttributeOnlyOperation', class: AttributeOnlyOperation::class);
216+
$this->assertEquals(new ResourceMetadataCollection(AttributeOnlyOperation::class, [
217+
new ApiResource(
218+
shortName: 'AttributeOnlyOperation',
219+
class: AttributeOnlyOperation::class,
220+
operations: [
221+
'my own name' => (new Get(name: 'my own name', priority: 1))->withOperation($operation),
222+
]
223+
),
224+
]), $attributeResourceMetadataCollectionFactory->create(AttributeOnlyOperation::class));
225+
}
209226
}

0 commit comments

Comments
 (0)