From 8527d8d022946a41bef01f5d73a24ce3b61a7b58 Mon Sep 17 00:00:00 2001 From: LESENECHAL Bruno Date: Thu, 31 Aug 2017 14:48:20 +0200 Subject: [PATCH] Add itemsPerPage (if needed) to swagger --- .../Bundle/Resources/config/swagger.xml | 2 ++ .../Serializer/DocumentationNormalizer.php | 26 ++++++++++++++++++- .../DocumentationNormalizerTest.php | 16 +++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index c45b8a2c37d..9f39e248fab 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -25,6 +25,8 @@ %api_platform.collection.pagination.enabled% %api_platform.collection.pagination.page_parameter_name% + %api_platform.collection.pagination.client_items_per_page% + %api_platform.collection.pagination.items_per_page_parameter_name% diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 82a9b12f3f5..1177d6689d1 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -62,11 +62,13 @@ final class DocumentationNormalizer implements NormalizerInterface private $subresourceOperationFactory; private $paginationEnabled; private $paginationPageParameterName; + private $clientItemsPerPage; + private $itemsPerPageParameterName; /** * @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection */ - public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, $oauthEnabled = false, $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, $paginationEnabled = true, $paginationPageParameterName = 'page') + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, $oauthEnabled = false, $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, $paginationEnabled = true, $paginationPageParameterName = 'page', $clientItemsPerPage = false, $itemsPerPageParameterName = 'itemsPerPage') { if ($urlGenerator) { @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0.', UrlGeneratorInterface::class, __METHOD__), E_USER_DEPRECATED); @@ -90,6 +92,8 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->subresourceOperationFactory = $subresourceOperationFactory; $this->paginationEnabled = $paginationEnabled; $this->paginationPageParameterName = $paginationPageParameterName; + $this->clientItemsPerPage = $clientItemsPerPage; + $this->itemsPerPageParameterName = $itemsPerPageParameterName; } /** @@ -286,6 +290,10 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType if ($this->paginationEnabled && $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', true, true)) { $pathOperation['parameters'][] = $this->getPaginationParameters(); + + if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { + $pathOperation['parameters'][] = $this->getItemsParPageParameters(); + } } return $pathOperation; @@ -689,6 +697,22 @@ private function getPaginationParameters(): array ]; } + /** + * Returns items per page parameters for the "get" collection operation. + * + * @return array + */ + private function getItemsParPageParameters(): array + { + return [ + 'name' => $this->itemsPerPageParameterName, + 'in' => 'query', + 'required' => false, + 'type' => 'integer', + 'description' => 'The number of items per page', + ]; + } + /** * {@inheritdoc} */ diff --git a/tests/Swagger/Serializer/DocumentationNormalizerTest.php b/tests/Swagger/Serializer/DocumentationNormalizerTest.php index fce651cbe4d..f394ac9e856 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerTest.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerTest.php @@ -83,7 +83,7 @@ public function testNormalize() $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); $propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name'])); - $dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST'], 'custom' => ['method' => 'GET', 'path' => '/foo'], 'custom2' => ['method' => 'POST', 'path' => '/foo']], []); + $dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', ['get' => ['method' => 'GET'], 'put' => ['method' => 'PUT']], ['get' => ['method' => 'GET'], 'post' => ['method' => 'POST'], 'custom' => ['method' => 'GET', 'path' => '/foo'], 'custom2' => ['method' => 'POST', 'path' => '/foo']], ['pagination_client_items_per_page' => true]); $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); $resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata); @@ -135,6 +135,13 @@ public function testNormalize() 'type' => 'integer', 'description' => 'The collection page number', ], + [ + 'name' => 'itemsPerPage', + 'in' => 'query', + 'required' => false, + 'type' => 'integer', + 'description' => 'The number of items per page', + ], ], 'responses' => [ 200 => [ @@ -236,6 +243,13 @@ public function testNormalize() 'type' => 'integer', 'description' => 'The collection page number', ], + [ + 'name' => 'itemsPerPage', + 'in' => 'query', + 'required' => false, + 'type' => 'integer', + 'description' => 'The number of items per page', + ], ], 'responses' => [ 200 => [