Skip to content

Commit 83dbfbf

Browse files
fix(metadata): generated NotExposed operation should inherit resource options (#5722)
1 parent 4dcfc16 commit 83dbfbf

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Metadata/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*/
2929
final class NotExposedOperationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
3030
{
31+
use OperationDefaultsTrait;
32+
3133
public static $skolemUriTemplate = '/.well-known/genid/{id}';
3234

3335
private $linkFactory;
@@ -69,13 +71,13 @@ public function create(string $resourceClass): ResourceMetadataCollection
6971
// No item operation has been found on all resources for resource class: generate one on the last resource
7072
// Helpful to generate an IRI for a resource without declaring the Get operation
7173
/** @var HttpOperation $operation */
72-
$operation = (new NotExposed())->withClass($resource->getClass())->withShortName($resource->getShortName()); // @phpstan-ignore-line $resource is defined if count > 0
74+
[$key, $operation] = $this->getOperationWithDefaults($resource, new NotExposed(), true, ['uriTemplate']); // @phpstan-ignore-line $resource is defined if count > 0
7375

7476
if (!$this->linkFactory->createLinksFromIdentifiers($operation)) {
7577
$operation = $operation->withUriTemplate(self::$skolemUriTemplate);
7678
}
7779

78-
$operations->add(sprintf('_api_%s_get', $operation->getShortName()), $operation)->sort(); // @phpstan-ignore-line $operation exists
80+
$operations->add($key, $operation)->sort(); // @phpstan-ignore-line $operation exists
7981

8082
return $resourceMetadataCollection;
8183
}

src/Metadata/Resource/Factory/OperationDefaultsTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,18 @@ private function completeGraphQlOperations(ApiResource $resource): ApiResource
154154
return $resource->withGraphQlOperations($graphQlOperations);
155155
}
156156

157-
private function getOperationWithDefaults(ApiResource $resource, Operation $operation, bool $generated = false): array
157+
private function getOperationWithDefaults(ApiResource $resource, Operation $operation, bool $generated = false, array $ignoredOptions = []): array
158158
{
159159
// Inherit from resource defaults
160160
foreach (get_class_methods($resource) as $methodName) {
161161
if (!str_starts_with($methodName, 'get')) {
162162
continue;
163163
}
164164

165+
if (\in_array(lcfirst(substr($methodName, 3)), $ignoredOptions, true)) {
166+
continue;
167+
}
168+
165169
if (!method_exists($operation, $methodName) || null !== $operation->{$methodName}()) {
166170
continue;
167171
}
@@ -203,7 +207,6 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
203207
$operation = $operation->withName($operation->getRouteName());
204208
}
205209

206-
$path = ($operation->getRoutePrefix() ?? '').($operation->getUriTemplate() ?? '');
207210
$operationName = $operation->getName() ?? $this->getDefaultOperationName($operation, $resource->getClass());
208211

209212
return [

src/Metadata/Tests/Resource/Factory/NotExposedOperationResourceMetadataCollectionFactoryTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class: AttributeResource::class
182182
shortName: 'AttributeResource',
183183
operations: [
184184
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
185-
'_api_AttributeResource_get' => new NotExposed(controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false),
185+
'_api_AttributeResource_get' => new NotExposed(controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false, extraProperties: ['generated_operation' => true]),
186186
],
187187
class: AttributeResource::class
188188
),
@@ -206,6 +206,8 @@ class: AttributeResource::class
206206
),
207207
new ApiResource(
208208
shortName: 'AttributeResource',
209+
types: ['https://schema.org/Book'],
210+
uriTemplate: '/custom_api_resources', // uriTemplate should not be inherited on NotExposed operation
209211
operations: [
210212
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
211213
],
@@ -224,9 +226,11 @@ class: AttributeResource::class
224226
),
225227
new ApiResource(
226228
shortName: 'AttributeResource',
229+
uriTemplate: '/custom_api_resources',
230+
types: ['https://schema.org/Book'],
227231
operations: [
228232
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
229-
'_api_AttributeResource_get' => new NotExposed(uriTemplate: '/.well-known/genid/{id}', controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false),
233+
'_api_AttributeResource_get' => new NotExposed(uriTemplate: '/.well-known/genid/{id}', controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false, extraProperties: ['generated_operation' => true], types: ['https://schema.org/Book']),
230234
],
231235
class: AttributeResource::class
232236
),

0 commit comments

Comments
 (0)