Skip to content

Commit

Permalink
fix(metadata): define a name on a single operation (#5090)
Browse files Browse the repository at this point in the history
fixes #5082
  • Loading branch information
soyuka authored Oct 25, 2022
1 parent 310363d commit 8250d41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
}

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

Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/Entity/AttributeOnlyOperation.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\Entity;

use ApiPlatform\Metadata\Get;

#[Get(name: 'my own name')]
final class AttributeOnlyOperation
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResources;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ExtraPropertiesResource;
Expand Down Expand Up @@ -206,4 +207,20 @@ public function testExtraProperties(): void
$this->assertEquals($extraPropertiesResource[0]->getExtraProperties(), ['foo' => 'bar']);
$this->assertEquals($extraPropertiesResource->getOperation('_api_ExtraPropertiesResource_get')->getExtraProperties(), ['foo' => 'bar']);
}

public function testOverrideNameWithoutOperations(): void
{
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory();

$operation = new HttpOperation(shortName: 'AttributeOnlyOperation', class: AttributeOnlyOperation::class);
$this->assertEquals(new ResourceMetadataCollection(AttributeOnlyOperation::class, [
new ApiResource(
shortName: 'AttributeOnlyOperation',
class: AttributeOnlyOperation::class,
operations: [
'my own name' => (new Get(name: 'my own name', priority: 1))->withOperation($operation),
]
),
]), $attributeResourceMetadataCollectionFactory->create(AttributeOnlyOperation::class));
}
}

0 comments on commit 8250d41

Please sign in to comment.