Skip to content

Commit 7834b0f

Browse files
committed
Merge branch 'master' of github.com:api-platform/core
2 parents 53ff909 + fd93636 commit 7834b0f

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

src/Bridge/Symfony/Bundle/Resources/config/swagger.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<argument>%api_platform.collection.pagination.page_parameter_name%</argument>
2929
<argument>%api_platform.collection.pagination.client_items_per_page%</argument>
3030
<argument>%api_platform.collection.pagination.items_per_page_parameter_name%</argument>
31+
<argument>%api_platform.collection.pagination.client_enabled%</argument>
32+
<argument>%api_platform.collection.pagination.enabled_parameter_name%</argument>
3133
<tag name="serializer.normalizer" priority="16" />
3234
</service>
3335

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ final class DocumentationNormalizer implements NormalizerInterface, CacheableSup
6868
private $paginationPageParameterName;
6969
private $clientItemsPerPage;
7070
private $itemsPerPageParameterName;
71+
private $paginationClientEnabled;
72+
private $paginationClientEnabledParameterName;
7173

7274
/**
7375
* @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection
7476
*/
75-
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 = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, $paginationEnabled = true, $paginationPageParameterName = 'page', $clientItemsPerPage = false, $itemsPerPageParameterName = 'itemsPerPage')
77+
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 = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, $paginationEnabled = true, $paginationPageParameterName = 'page', $clientItemsPerPage = false, $itemsPerPageParameterName = 'itemsPerPage', $paginationClientEnabled = false, $paginationClientEnabledParameterName = 'pagination')
7678
{
7779
if ($urlGenerator) {
7880
@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);
@@ -100,6 +102,8 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
100102
$this->subresourceOperationFactory = $subresourceOperationFactory;
101103
$this->clientItemsPerPage = $clientItemsPerPage;
102104
$this->itemsPerPageParameterName = $itemsPerPageParameterName;
105+
$this->paginationClientEnabled = $paginationClientEnabled;
106+
$this->paginationClientEnabledParameterName = $paginationClientEnabledParameterName;
103107
}
104108

105109
/**
@@ -275,6 +279,9 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType
275279
$pathOperation['parameters'][] = $this->getItemsPerPageParameters();
276280
}
277281
}
282+
if ($this->paginationEnabled && $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->paginationClientEnabled, true)) {
283+
$pathOperation['parameters'][] = $this->getPaginationClientEnabledParameters();
284+
}
278285

279286
return $pathOperation;
280287
}
@@ -635,6 +642,20 @@ private function getPaginationParameters(): array
635642
];
636643
}
637644

645+
/**
646+
* Returns enable pagination parameter for the "get" collection operation.
647+
*/
648+
private function getPaginationClientEnabledParameters(): array
649+
{
650+
return [
651+
'name' => $this->paginationClientEnabledParameterName,
652+
'in' => 'query',
653+
'required' => false,
654+
'type' => 'boolean',
655+
'description' => 'Enable or disable pagination',
656+
];
657+
}
658+
638659
/**
639660
* Returns items per page parameters for the "get" collection operation.
640661
*/

tests/Swagger/Serializer/DocumentationNormalizerTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,4 +1976,103 @@ public function testNormalizeWithPropertySwaggerContext()
19761976

19771977
$this->assertEquals($expected, $normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['base_url' => '/app_dev.php/']));
19781978
}
1979+
1980+
public function testNormalizeWithPaginationClientEnabled()
1981+
{
1982+
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3', ['jsonld' => ['application/ld+json']]);
1983+
1984+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
1985+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name']));
1986+
1987+
$dummyMetadata = new ResourceMetadata('Dummy', 'This is a dummy.', 'http://schema.example.com/Dummy', [], ['get' => ['method' => 'GET', 'pagination_client_enabled' => true]]);
1988+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
1989+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
1990+
1991+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
1992+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
1993+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a name.', true, true, true, true, false, false, null, null, ['swagger_context' => ['type' => 'string', 'enum' => ['one', 'two'], 'example' => 'one']]));
1994+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
1995+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
1996+
1997+
$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
1998+
$operationMethodResolverProphecy->getCollectionOperationMethod(Dummy::class, 'get')->shouldBeCalled()->willReturn('GET');
1999+
2000+
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
2001+
2002+
$normalizer = new DocumentationNormalizer(
2003+
$resourceMetadataFactoryProphecy->reveal(),
2004+
$propertyNameCollectionFactoryProphecy->reveal(),
2005+
$propertyMetadataFactoryProphecy->reveal(),
2006+
$resourceClassResolverProphecy->reveal(),
2007+
$operationMethodResolverProphecy->reveal(),
2008+
$operationPathResolver
2009+
);
2010+
2011+
$expected = [
2012+
'swagger' => '2.0',
2013+
'basePath' => '/app_dev.php/',
2014+
'info' => [
2015+
'title' => 'Test API',
2016+
'description' => 'This is a test API.',
2017+
'version' => '1.2.3',
2018+
],
2019+
'paths' => new \ArrayObject([
2020+
'/dummies' => [
2021+
'get' => new \ArrayObject([
2022+
'tags' => ['Dummy'],
2023+
'operationId' => 'getDummyCollection',
2024+
'produces' => ['application/ld+json'],
2025+
'summary' => 'Retrieves the collection of Dummy resources.',
2026+
'parameters' => [
2027+
[
2028+
'name' => 'page',
2029+
'in' => 'query',
2030+
'required' => false,
2031+
'type' => 'integer',
2032+
'description' => 'The collection page number',
2033+
],
2034+
[
2035+
'name' => 'pagination',
2036+
'in' => 'query',
2037+
'required' => false,
2038+
'type' => 'boolean',
2039+
'description' => 'Enable or disable pagination',
2040+
],
2041+
],
2042+
'responses' => [
2043+
200 => [
2044+
'description' => 'Dummy collection response',
2045+
'schema' => [
2046+
'type' => 'array',
2047+
'items' => ['$ref' => '#/definitions/Dummy'],
2048+
],
2049+
],
2050+
],
2051+
]),
2052+
],
2053+
]),
2054+
'definitions' => new \ArrayObject([
2055+
'Dummy' => new \ArrayObject([
2056+
'type' => 'object',
2057+
'description' => 'This is a dummy.',
2058+
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
2059+
'properties' => [
2060+
'id' => new \ArrayObject([
2061+
'type' => 'integer',
2062+
'description' => 'This is an id.',
2063+
'readOnly' => true,
2064+
]),
2065+
'name' => new \ArrayObject([
2066+
'type' => 'string',
2067+
'description' => 'This is a name.',
2068+
'enum' => ['one', 'two'],
2069+
'example' => 'one',
2070+
]),
2071+
],
2072+
]),
2073+
]),
2074+
];
2075+
2076+
$this->assertEquals($expected, $normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['base_url' => '/app_dev.php/']));
2077+
}
19792078
}

0 commit comments

Comments
 (0)