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

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
fix(metadata): generated NotExposed operation should inherit resource…
… options
  • Loading branch information
vincentchalamon committed Aug 4, 2023
commit 26c843e9ef0b9e1af6f257be1f878291b430a99d
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
final class NotExposedOperationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
use OperationDefaultsTrait;

public static $skolemUriTemplate = '/.well-known/genid/{id}';

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

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

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

return $resourceMetadataCollection;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Metadata/Resource/Factory/OperationDefaultsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,18 @@ private function completeGraphQlOperations(ApiResource $resource): ApiResource
return $resource->withGraphQlOperations($graphQlOperations);
}

private function getOperationWithDefaults(ApiResource $resource, Operation $operation, bool $generated = false): array
private function getOperationWithDefaults(ApiResource $resource, Operation $operation, bool $generated = false, array $ignoredOptions = []): array
{
// Inherit from resource defaults
foreach (get_class_methods($resource) as $methodName) {
if (!str_starts_with($methodName, 'get')) {
continue;
}

if (\in_array(lcfirst(substr($methodName, 3)), $ignoredOptions, true)) {
continue;
}

if (!method_exists($operation, $methodName) || null !== $operation->{$methodName}()) {
continue;
}
Expand Down Expand Up @@ -203,7 +207,6 @@ private function getOperationWithDefaults(ApiResource $resource, Operation $oper
$operation = $operation->withName($operation->getRouteName());
}

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

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class: AttributeResource::class
shortName: 'AttributeResource',
operations: [
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
'_api_AttributeResource_get' => new NotExposed(controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false),
'_api_AttributeResource_get' => new NotExposed(controller: 'api_platform.action.not_exposed', shortName: 'AttributeResource', class: AttributeResource::class, output: false, read: false, extraProperties: ['generated_operation' => true]),
],
class: AttributeResource::class
),
Expand All @@ -206,6 +206,8 @@ class: AttributeResource::class
),
new ApiResource(
shortName: 'AttributeResource',
types: ['https://schema.org/Book'],
uriTemplate: '/custom_api_resources', // uriTemplate should not be inherited on NotExposed operation
operations: [
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
],
Expand All @@ -224,9 +226,11 @@ class: AttributeResource::class
),
new ApiResource(
shortName: 'AttributeResource',
uriTemplate: '/custom_api_resources',
types: ['https://schema.org/Book'],
operations: [
'_api_AttributeResource_get_collection' => new GetCollection(controller: 'api_platform.action.placeholder', shortName: 'AttributeResource', class: AttributeResource::class),
'_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),
'_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']),
],
class: AttributeResource::class
),
Expand Down