Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d38cd9

Browse files
authored
fix(metadata): include routePrefix in default operation name (#5203) (#5252)
1 parent 6a93263 commit 6d38cd9

5 files changed

Lines changed: 57 additions & 21 deletions

File tree

src/Metadata/Resource/Factory/OperationDefaultsTrait.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,28 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
187187
$operation = $operation->withName($operation->getRouteName());
188188
}
189189

190-
$operationName = $operation->getName() ?? sprintf(
191-
'_api_%s_%s%s',
192-
$operation->getUriTemplate() ?: $operation->getShortName(),
193-
strtolower($operation->getMethod() ?? HttpOperation::METHOD_GET),
194-
$operation instanceof CollectionOperationInterface ? '_collection' : '',
195-
);
190+
$path = ($operation->getRoutePrefix() ?? '').($operation->getUriTemplate() ?? '');
191+
$operationName = $operation->getName() ?? $this->getDefaultOperationName($operation, $resource->getClass());
196192

197193
return [
198194
$operationName,
199195
$operation,
200196
];
201197
}
198+
199+
private function getDefaultShortname(string $resourceClass): string
200+
{
201+
return (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
202+
}
203+
204+
private function getDefaultOperationName(HttpOperation $operation, string $resourceClass): string
205+
{
206+
$path = ($operation->getRoutePrefix() ?? '').($operation->getUriTemplate() ?? '');
207+
208+
return sprintf(
209+
'_api_%s_%s%s',
210+
$path ?: ($operation->getShortName() ?? $this->getDefaultShortname($resourceClass)),
211+
strtolower($operation->getMethod() ?? HttpOperation::METHOD_GET),
212+
$operation instanceof CollectionOperationInterface ? '_collection' : '');
213+
}
202214
}

src/Metadata/Resource/Factory/OperationNameResourceMetadataCollectionFactory.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace ApiPlatform\Metadata\Resource\Factory;
1515

16-
use ApiPlatform\Metadata\CollectionOperationInterface;
17-
use ApiPlatform\Metadata\HttpOperation;
1816
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
1917

2018
/**
@@ -24,6 +22,8 @@
2422
*/
2523
final class OperationNameResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2624
{
25+
use OperationDefaultsTrait;
26+
2727
public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null)
2828
{
2929
}
@@ -52,8 +52,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
5252
continue;
5353
}
5454

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

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

6362
return $resourceMetadataCollection;
6463
}
65-
66-
private function getDefaultShortname(string $resourceClass): string
67-
{
68-
return (false !== $pos = strrpos($resourceClass, '\\')) ? substr($resourceClass, $pos + 1) : $resourceClass;
69-
}
7064
}

src/Metadata/Resource/Factory/UriTemplateResourceMetadataCollectionFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
final class UriTemplateResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2929
{
30+
use OperationDefaultsTrait;
31+
3032
private $triggerLegacyFormatOnce = [];
3133

3234
public function __construct(private readonly LinkFactoryInterface $linkFactory, private readonly PathSegmentNameGeneratorInterface $pathSegmentNameGenerator, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null)
@@ -78,12 +80,12 @@ public function create(string $resourceClass): ResourceMetadataCollection
7880
}
7981

8082
$operation = $operation->withUriTemplate($this->generateUriTemplate($operation));
81-
$operationName = $operation->getName() ?: sprintf('_api_%s_%s%s', $operation->getUriTemplate(), strtolower($operation->getMethod() ?? HttpOperation::METHOD_GET), $operation instanceof CollectionOperationInterface ? '_collection' : '');
83+
8284
if (!$operation->getName()) {
83-
$operation = $operation->withName($operationName);
85+
$operation = $operation->withName($this->getDefaultOperationName($operation, $resourceClass));
8486
}
8587

86-
$operations->add($operationName, $operation);
88+
$operations->add($operation->getName(), $operation);
8789
}
8890

8991
$resource = $resource->withOperations($operations->sort());

tests/Metadata/Extractor/ResourceMetadataCompatibilityTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\Tests\Metadata\Extractor;
1515

1616
use ApiPlatform\Metadata\ApiResource;
17-
use ApiPlatform\Metadata\CollectionOperationInterface;
1817
use ApiPlatform\Metadata\Delete;
1918
use ApiPlatform\Metadata\Extractor\XmlResourceExtractor;
2019
use ApiPlatform\Metadata\Extractor\YamlResourceExtractor;
@@ -474,7 +473,7 @@ private function buildApiResources(): array
474473
// Build default operations
475474
$operations = [];
476475
foreach ([new Get(), new GetCollection(), new Post(), new Put(), new Patch(), new Delete()] as $operation) {
477-
$operationName = sprintf('_api_%s_%s%s', $resource->getShortName(), strtolower($operation->getMethod()), $operation instanceof CollectionOperationInterface ? '_collection' : '');
476+
$operationName = $this->getDefaultOperationName($operation, self::RESOURCE_CLASS);
478477
[$name, $operation] = $this->getOperationWithDefaults($resource, $operation);
479478
$operations[$name] = $operation;
480479
}
@@ -572,7 +571,7 @@ private function withOperations(array $values, ?array $fixtures): Operations
572571
throw new \RuntimeException(sprintf('Unknown Operation parameter "%s".', $parameter));
573572
}
574573

575-
$operationName = $operation->getName() ?? sprintf('_api_%s_%s%s', $operation->getUriTemplate() ?: $operation->getShortName(), strtolower($operation->getMethod()), $operation instanceof CollectionOperationInterface ? '_collection' : '');
574+
$operationName = $operation->getName() ?? $this->getDefaultOperationName($operation, self::RESOURCE_CLASS);
576575
$operations[$operationName] = $operation;
577576
}
578577

tests/Metadata/Resource/Factory/UriTemplateResourceMetadataCollectionFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ class: AttributeResource::class,
107107
uriTemplate: '/dummy/{dummyId}/attribute_resources/{id}',
108108
uriVariables: ['dummyId' => ['from_class' => Dummy::class, 'identifiers' => ['id']], 'id' => ['from_class' => AttributeResource::class, 'identifiers' => ['id']]],
109109
),
110+
new ApiResource(
111+
shortName: 'AttributeResource',
112+
class: AttributeResource::class,
113+
uriVariables: ['id' => new Link(fromClass: AttributeResource::class, identifiers: ['id'])],
114+
operations: [
115+
new Get(
116+
shortName: 'AttributeResource',
117+
class: AttributeResource::class,
118+
controller: 'api_platform.action.placeholder',
119+
uriVariables: ['id' => new Link(fromClass: AttributeResource::class, identifiers: ['id'])],
120+
routePrefix: '/prefix',
121+
),
122+
]
123+
),
110124
]),
111125
);
112126

@@ -169,6 +183,21 @@ class: AttributeResource::class,
169183
uriVariables: ['dummyId' => new Link(fromClass: Dummy::class, identifiers: ['id'], parameterName: 'dummyId'), 'id' => new Link(fromClass: AttributeResource::class, identifiers: ['id'], parameterName: 'id')],
170184
operations: [],
171185
),
186+
new ApiResource(
187+
uriVariables: ['id' => new Link(fromClass: AttributeResource::class, identifiers: ['id'], parameterName: 'id')],
188+
shortName: 'AttributeResource',
189+
class: AttributeResource::class,
190+
operations: [
191+
'_api_/prefix/attribute_resources/{id}{._format}_get' => new Get(
192+
uriTemplate: '/attribute_resources/{id}{._format}',
193+
shortName: 'AttributeResource',
194+
class: AttributeResource::class,
195+
controller: 'api_platform.action.placeholder',
196+
uriVariables: ['id' => new Link(fromClass: AttributeResource::class, identifiers: ['id'], parameterName: 'id')],
197+
routePrefix: '/prefix',
198+
name: '_api_/prefix/attribute_resources/{id}{._format}_get'),
199+
]
200+
),
172201
]),
173202
$uriTemplateResourceMetadataCollectionFactory->create(AttributeResource::class)
174203
);

0 commit comments

Comments
 (0)