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
11 changes: 0 additions & 11 deletions src/Metadata/Resource/Factory/OperationDefaultsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,6 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
$operation = $operation->withName($operation->getRouteName());
}

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

$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()));
/** @var HttpOperation $operation */
$operation = $operation->withName('');
}

$operationName = $operation->getName() ?? sprintf(
'_api_%s_%s%s',
$operation->getUriTemplate() ?: $operation->getShortName(),
Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/PasswordResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Post;

#[
ApiResource(
shortName: 'PasswordResource',
operations: [
new Post(
uriTemplate: '/password/reset',
name: 'password_reset',
),
new Post(
uriTemplate: '/password/set',
name: 'password_set',
),
new Get(
uriTemplate: '/password/reset/{token}',
name: 'password_reset_token',
),
],
routePrefix: '/users'
)
]
/**
* Test for issue #5235.
*/
class PasswordResource
{
#[ApiProperty(identifier: true)]
public ?string $token = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\PasswordResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeDefaultOperations;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeOnlyOperation;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\AttributeResource;
Expand Down Expand Up @@ -226,4 +227,18 @@ class: AttributeOnlyOperation::class,
),
]), $attributeResourceMetadataCollectionFactory->create(AttributeOnlyOperation::class));
}

/**
* Tests issue #5235.
*/
public function testNameDeclarationShouldNotBeRemoved(): void
{
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory();

$metadataCollection = $attributeResourceMetadataCollectionFactory->create(PasswordResource::class);
$operations = $metadataCollection[0]->getOperations();
$this->assertTrue($operations->has('password_set'));
$this->assertTrue($operations->has('password_reset_token'));
$this->assertTrue($operations->has('password_reset'));
}
}