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
5 changes: 5 additions & 0 deletions src/Metadata/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function __construct(array $operations = [])
// When we use an int-indexed array in the constructor, compute priorities
if (\is_int($operationName)) {
$operation = $operation->withPriority($operationName);
$operationName = (string) $operationName;
}

if ($operation->getName()) {
$operationName = $operation->getName();
}

$this->operations[] = [$operationName, $operation];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function create(string $resourceClass): ResourceMetadataCollection
continue;
}

$newOperationName = sprintf('_api_%s_%s%s', $operation->getUriTemplate() ?: $operation->getShortName(), strtolower($operation->getMethod() ?? HttpOperation::METHOD_GET), $operation instanceof CollectionOperationInterface ? '_collection' : '');
$path = ($operation->getRoutePrefix() ?? '').($operation->getUriTemplate() ?? '');
$newOperationName = sprintf('_api_%s_%s%s', $path ?: ($operation->getShortName() ?? $this->getDefaultShortname($resourceClass)), strtolower($operation->getMethod() ?? HttpOperation::METHOD_GET), $operation instanceof CollectionOperationInterface ? '_collection' : '');
$operations->remove($operationName)->add($newOperationName, $operation->withName($newOperationName));
}

Expand All @@ -61,4 +62,9 @@ public function create(string $resourceClass): ResourceMetadataCollection

return $resourceMetadataCollection;
}

private function getDefaultShortname(string $resourceClass): string
{
return (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Metadata\Resource\Factory;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\OperationNameResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class OperationNameResourceMetadataFactoryTest extends TestCase
{
use ProphecyTrait;

/**
* @dataProvider operationProvider
*/
public function testGeneratesName(Operation $operation, string $expectedOperationName): void
{
$decorated = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$decorated->create('a')->willReturn(new ResourceMetadataCollection('a', [
new ApiResource(operations: [$operation]),
]));

$operationNameResourceMetadataFactory = new OperationNameResourceMetadataCollectionFactory($decorated->reveal());
$result = $operationNameResourceMetadataFactory->create('a');

$this->assertEquals($operation->withName($expectedOperationName), $result->getOperation($expectedOperationName));
}

public function operationProvider(): array
{
return [
[new Get(), '_api_a_get'],
[new Get(shortName: 'Foo'), '_api_Foo_get'],
[new Get(name: 'test'), 'test'],
[new Get(routePrefix: 'foo'), '_api_foo_get'],
[new Get(uriTemplate: '/foo'), '_api_/foo_get'],
[new Get(routePrefix: '/admin', uriTemplate: '/foo'), '_api_/admin/foo_get'],
];
}
}