Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}