From 54a378d151409ef6ec8fb7cfea6b9c74e5115d44 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Fri, 23 Jun 2023 17:40:39 +0200 Subject: [PATCH] fix(OpenApi): Fixed `getByPath` operation overlap with `get` by setting `id` request attribute and api_resource operation configuration --- config/api_resources/nsarticle.yml | 6 ++- config/api_resources/nsoffer.yml | 6 ++- config/api_resources/nspage.yml | 6 ++- lib/RoadizCoreBundle/config/services.yaml | 5 +++ .../GetWebResponseByPathController.php | 4 ++ .../src/Api/OpenApi/WebResponseDecorator.php | 39 +++++++++++++++++++ .../src/NodeType/ApiResourceGenerator.php | 4 ++ 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/RoadizCoreBundle/src/Api/OpenApi/WebResponseDecorator.php diff --git a/config/api_resources/nsarticle.yml b/config/api_resources/nsarticle.yml index e5200f65..58176016 100644 --- a/config/api_resources/nsarticle.yml +++ b/config/api_resources/nsarticle.yml @@ -28,7 +28,11 @@ App\GeneratedEntity\NSArticle: - document_thumbnails - document_display_sources getByPath: - method: GET + method: 'GET' + path: '/web_response_by_path' + read: false + controller: RZ\Roadiz\CoreBundle\Api\Controller\GetWebResponseByPathController + pagination_enabled: false normalization_context: enable_max_depth: true groups: diff --git a/config/api_resources/nsoffer.yml b/config/api_resources/nsoffer.yml index 4c619976..bd2c37d2 100644 --- a/config/api_resources/nsoffer.yml +++ b/config/api_resources/nsoffer.yml @@ -28,7 +28,11 @@ App\GeneratedEntity\NSOffer: - document_thumbnails - document_display_sources getByPath: - method: GET + method: 'GET' + path: '/web_response_by_path' + read: false + controller: RZ\Roadiz\CoreBundle\Api\Controller\GetWebResponseByPathController + pagination_enabled: false normalization_context: enable_max_depth: true groups: diff --git a/config/api_resources/nspage.yml b/config/api_resources/nspage.yml index 345c691c..ec5231db 100644 --- a/config/api_resources/nspage.yml +++ b/config/api_resources/nspage.yml @@ -32,7 +32,11 @@ App\GeneratedEntity\NSPage: - nodes_sources_images - nodes_sources_boolean getByPath: - method: GET + method: 'GET' + path: '/web_response_by_path' + read: false + controller: RZ\Roadiz\CoreBundle\Api\Controller\GetWebResponseByPathController + pagination_enabled: false normalization_context: enable_max_depth: true groups: diff --git a/lib/RoadizCoreBundle/config/services.yaml b/lib/RoadizCoreBundle/config/services.yaml index 617438f6..c3e2cf83 100644 --- a/lib/RoadizCoreBundle/config/services.yaml +++ b/lib/RoadizCoreBundle/config/services.yaml @@ -124,6 +124,11 @@ services: arguments: [ '@RZ\Roadiz\CoreBundle\Api\OpenApi\JwtDecorator.inner' ] autoconfigure: false + RZ\Roadiz\CoreBundle\Api\OpenApi\WebResponseDecorator: + decorates: 'api_platform.openapi.factory' + arguments: [ '@RZ\Roadiz\CoreBundle\Api\OpenApi\WebResponseDecorator.inner' ] + autoconfigure: false + # # API Platform normalizers # diff --git a/lib/RoadizCoreBundle/src/Api/Controller/GetWebResponseByPathController.php b/lib/RoadizCoreBundle/src/Api/Controller/GetWebResponseByPathController.php index 9b0a9b80..a6513e47 100644 --- a/lib/RoadizCoreBundle/src/Api/Controller/GetWebResponseByPathController.php +++ b/lib/RoadizCoreBundle/src/Api/Controller/GetWebResponseByPathController.php @@ -54,7 +54,11 @@ public function __invoke(): ?WebResponseInterface $resource = $this->normalizeResourcePath( (string) $this->requestStack->getMainRequest()->query->get('path') ); + if (null === $resource) { + throw new ResourceNotFoundException('Resource not found'); + } $this->requestStack->getMainRequest()->attributes->set('data', $resource); + $this->requestStack->getMainRequest()->attributes->set('id', $resource->getId()); /* * Force API Platform to look for real resource configuration and serialization * context. You must define "itemOperations.getByPath" for your API resource configuration. diff --git a/lib/RoadizCoreBundle/src/Api/OpenApi/WebResponseDecorator.php b/lib/RoadizCoreBundle/src/Api/OpenApi/WebResponseDecorator.php new file mode 100644 index 00000000..c44d1c2f --- /dev/null +++ b/lib/RoadizCoreBundle/src/Api/OpenApi/WebResponseDecorator.php @@ -0,0 +1,39 @@ +decorated = $decorated; + } + + public function __invoke(array $context = []): OpenApi + { + $openApi = ($this->decorated)($context); + $schemas = $openApi->getComponents()->getSchemas(); + $pathItem = $openApi->getPaths()->getPath('/api/web_response_by_path'); + $operation = $pathItem->getGet(); + + $openApi->getPaths()->addPath('/api/web_response_by_path', $pathItem->withGet( + $operation->withParameters([new Model\Parameter( + 'path', + 'query', + 'Resource path, or `/` for home page', + true, + )]) + )); + + return $openApi; + } +} diff --git a/lib/RoadizCoreBundle/src/NodeType/ApiResourceGenerator.php b/lib/RoadizCoreBundle/src/NodeType/ApiResourceGenerator.php index 428b6eb5..f4766afb 100644 --- a/lib/RoadizCoreBundle/src/NodeType/ApiResourceGenerator.php +++ b/lib/RoadizCoreBundle/src/NodeType/ApiResourceGenerator.php @@ -138,6 +138,10 @@ protected function getItemOperations(NodeTypeInterface $nodeType): array if ($nodeType->isReachable()) { $operations['getByPath'] = [ 'method' => 'GET', + 'path' => '/web_response_by_path', + 'read' => 'false', + 'controller' => 'RZ\Roadiz\CoreBundle\Api\Controller\GetWebResponseByPathController', + 'pagination_enabled' => 'false', 'normalization_context' => [ 'enable_max_depth' => true, 'groups' => array_merge(array_values(array_filter(array_unique($groups))), [