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
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/swagger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<argument type="service" id="api_platform.subresource_operation_factory"></argument>
<argument>%api_platform.collection.pagination.enabled%</argument>
<argument>%api_platform.collection.pagination.page_parameter_name%</argument>
<argument>%api_platform.collection.pagination.client_items_per_page%</argument>
<argument>%api_platform.collection.pagination.items_per_page_parameter_name%</argument>
<tag name="serializer.normalizer" priority="16" />
</service>

Expand Down
26 changes: 25 additions & 1 deletion src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -90,6 +92,8 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
$this->subresourceOperationFactory = $subresourceOperationFactory;
$this->paginationEnabled = $paginationEnabled;
$this->paginationPageParameterName = $paginationPageParameterName;
$this->clientItemsPerPage = $clientItemsPerPage;
$this->itemsPerPageParameterName = $itemsPerPageParameterName;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down
16 changes: 15 additions & 1 deletion tests/Swagger/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 => [
Expand Down Expand Up @@ -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 => [
Expand Down