Skip to content

Commit 1e82e92

Browse files
committed
Add consistency for the maximum_items_per_page option.
1 parent a771df3 commit 1e82e92

File tree

8 files changed

+185
-18
lines changed

8 files changed

+185
-18
lines changed

src/Annotation/ApiResource.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @Attribute("input", type="mixed"),
4646
* @Attribute("iri", type="string"),
4747
* @Attribute("itemOperations", type="array"),
48-
* @Attribute("maximumItemsPerPage", type="int"),
4948
* @Attribute("mercure", type="mixed"),
5049
* @Attribute("messenger", type="mixed"),
5150
* @Attribute("normalizationContext", type="array"),
@@ -58,6 +57,8 @@
5857
* @Attribute("paginationEnabled", type="bool"),
5958
* @Attribute("paginationFetchJoinCollection", type="bool"),
6059
* @Attribute("paginationItemsPerPage", type="int"),
60+
* @Attribute("maximumItemsPerPage", type="int"),
61+
* @Attribute("paginationMaximumItemsPerPage", type="int"),
6162
* @Attribute("paginationPartial", type="bool"),
6263
* @Attribute("paginationViaCursor", type="array"),
6364
* @Attribute("routePrefix", type="string"),
@@ -198,13 +199,6 @@ final class ApiResource
198199
*/
199200
private $hydraContext;
200201

201-
/**
202-
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
203-
*
204-
* @var int
205-
*/
206-
private $maximumItemsPerPage;
207-
208202
/**
209203
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
210204
*
@@ -275,6 +269,22 @@ final class ApiResource
275269
*/
276270
private $paginationItemsPerPage;
277271

272+
/**
273+
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
274+
*
275+
* @var int
276+
*
277+
* @deprecated - Use $paginationMaximumItemsPerPage instead
278+
*/
279+
private $maximumItemsPerPage;
280+
281+
/**
282+
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
283+
*
284+
* @var int
285+
*/
286+
private $paginationMaximumItemsPerPage;
287+
278288
/**
279289
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
280290
*

src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ private function getPagination(QueryBuilder $queryBuilder, string $resourceClass
218218
}
219219

220220
if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) {
221-
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true);
221+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', null, true);
222+
223+
if (null !== $maxItemsPerPage) {
224+
@trigger_error('The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.', E_USER_DEPRECATED);
225+
}
226+
227+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_maximum_items_per_page', $maxItemsPerPage ?? $this->maximumItemPerPage, true);
228+
222229
$itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage);
223230
$itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage);
224231
}

src/DataProvider/Pagination.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,15 @@ public function getLimit(string $resourceClass = null, string $operationName = n
127127

128128
if ($clientLimit) {
129129
$limit = (int) $this->getParameterFromContext($context, $this->options['items_per_page_parameter_name'], $limit);
130-
$maxItemsPerPage = $this->options['maximum_items_per_page'];
130+
$maxItemsPerPage = null;
131131

132132
if (null !== $resourceClass) {
133133
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
134-
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $maxItemsPerPage, true);
134+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', null, true);
135+
if (null !== $maxItemsPerPage) {
136+
@trigger_error('The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.', E_USER_DEPRECATED);
137+
}
138+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_maximum_items_per_page', $maxItemsPerPage ?? $this->options['maximum_items_per_page'], true);
135139
}
136140

137141
if (null !== $maxItemsPerPage && $limit > $maxItemsPerPage) {

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,14 @@ private function addPaginationParameters(bool $v3, ResourceMetadata $resourceMet
379379
'minimum' => 0,
380380
];
381381

382-
if ($maximumItemPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', false, true)) {
383-
$itemPerPageParameter['schema']['maximum'] = $maximumItemPerPage;
382+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', null, true);
383+
if (null !== $maxItemsPerPage) {
384+
@trigger_error('The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.', E_USER_DEPRECATED);
385+
}
386+
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_maximum_items_per_page', $maxItemsPerPage, true);
387+
388+
if (null !== $maxItemsPerPage) {
389+
$itemPerPageParameter['schema']['maximum'] = $maxItemsPerPage;
384390
}
385391
} else {
386392
$itemPerPageParameter['type'] = 'integer';

tests/Annotation/ApiResourceTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public function testConstruct()
4343
'input' => 'Foo',
4444
'iri' => 'http://example.com/res',
4545
'itemOperations' => ['foo' => ['bar']],
46-
'maximumItemsPerPage' => 42,
4746
'mercure' => '[\'foo\', object.owner]',
4847
'messenger' => true,
4948
'normalizationContext' => ['groups' => ['bar']],
@@ -56,6 +55,8 @@ public function testConstruct()
5655
'paginationEnabled' => true,
5756
'paginationFetchJoinCollection' => true,
5857
'paginationItemsPerPage' => 42,
58+
'maximumItemsPerPage' => 42, // deprecated, see paginationMaximumItemsPerPage
59+
'paginationMaximumItemsPerPage' => 50,
5960
'paginationPartial' => true,
6061
'routePrefix' => '/foo',
6162
'shortName' => 'shortName',
@@ -84,7 +85,6 @@ public function testConstruct()
8485
'formats' => ['foo', 'bar' => ['application/bar']],
8586
'filters' => ['foo', 'bar'],
8687
'input' => 'Foo',
87-
'maximum_items_per_page' => 42,
8888
'mercure' => '[\'foo\', object.owner]',
8989
'messenger' => true,
9090
'normalization_context' => ['groups' => ['bar']],
@@ -97,6 +97,8 @@ public function testConstruct()
9797
'pagination_enabled' => true,
9898
'pagination_fetch_join_collection' => true,
9999
'pagination_items_per_page' => 42,
100+
'maximum_items_per_page' => 42,
101+
'pagination_maximum_items_per_page' => 50,
100102
'pagination_partial' => true,
101103
'route_prefix' => '/foo',
102104
'swagger_context' => ['description' => 'bar'],

tests/Bridge/Doctrine/MongoDbOdm/Extension/PaginationExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,15 @@ public function testApplyToCollectionWithMaximumItemsPerPage()
318318
$attributes = [
319319
'pagination_enabled' => true,
320320
'pagination_client_enabled' => true,
321-
'maximum_items_per_page' => 80,
321+
'pagination_maximum_items_per_page' => 80,
322322
];
323323
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata(null, null, null, [], [], $attributes));
324324
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
325325

326326
$pagination = new Pagination($resourceMetadataFactory, [
327327
'client_enabled' => true,
328328
'client_items_per_page' => true,
329-
'maximum_items_per_page' => 50,
329+
'pagination_maximum_items_per_page' => 50,
330330
]);
331331

332332
$aggregationBuilderProphecy = $this->mockAggregationBuilder(0, 80);

tests/Bridge/Doctrine/Orm/Extension/PaginationExtensionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ public function testApplyToCollectionWithMaximumItemsPerPage()
680680
$attributes = [
681681
'pagination_enabled' => true,
682682
'pagination_client_enabled' => true,
683-
'maximum_items_per_page' => 80,
683+
'pagination_maximum_items_per_page' => 80,
684684
];
685685
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata(null, null, null, [], [], $attributes));
686686
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
@@ -709,6 +709,7 @@ public function testApplyToCollectionWithMaximumItemsPerPage()
709709
* @expectedDeprecation Passing an instance of "Symfony\Component\HttpFoundation\RequestStack" as second argument of "ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\PaginationExtension" is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface" instead.
710710
* @expectedDeprecation Passing an instance of "ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface" as third argument of "ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\PaginationExtension" is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "ApiPlatform\Core\DataProvider\Pagination" instead.
711711
* @expectedDeprecation Passing "$enabled", "$clientEnabled", "$clientItemsPerPage", "$itemsPerPage", "$pageParameterName", "$enabledParameterName", "$itemsPerPageParameterName", "$maximumItemPerPage", "$partial", "$clientPartial", "$partialParameterName" arguments is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator" as third argument instead.
712+
* @expectedDeprecation The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.
712713
*/
713714
public function testLegacyApplyToCollectionWithMaximumItemsPerPage()
714715
{

tests/Swagger/Serializer/DocumentationNormalizerV3Test.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,6 +2598,143 @@ public function testNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): vo
25982598
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
25992599
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name']));
26002600

2601+
$dummyMetadata = new ResourceMetadata(
2602+
'Dummy',
2603+
'This is a dummy.',
2604+
'http://schema.example.com/Dummy',
2605+
[],
2606+
['get' => ['method' => 'GET'] + self::OPERATION_FORMATS],
2607+
['pagination_client_items_per_page' => true, 'pagination_items_per_page' => 20, 'pagination_maximum_items_per_page' => 80]
2608+
);
2609+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
2610+
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
2611+
2612+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
2613+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id')->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false));
2614+
$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, ['openapi_context' => ['type' => 'string', 'enum' => ['one', 'two'], 'example' => 'one']]));
2615+
2616+
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
2617+
2618+
$normalizer = new DocumentationNormalizer(
2619+
$resourceMetadataFactoryProphecy->reveal(),
2620+
$propertyNameCollectionFactoryProphecy->reveal(),
2621+
$propertyMetadataFactoryProphecy->reveal(),
2622+
null,
2623+
null,
2624+
$operationPathResolver,
2625+
null,
2626+
null,
2627+
null,
2628+
false,
2629+
'',
2630+
'',
2631+
'',
2632+
'',
2633+
[],
2634+
[],
2635+
null,
2636+
true,
2637+
'page',
2638+
false,
2639+
'itemsPerPage',
2640+
[],
2641+
false,
2642+
'pagination',
2643+
['spec_version' => 3]
2644+
);
2645+
2646+
$expected = [
2647+
'openapi' => '3.0.2',
2648+
'servers' => [['url' => '/app_dev.php/']],
2649+
'info' => [
2650+
'title' => 'Test API',
2651+
'description' => 'This is a test API.',
2652+
'version' => '1.2.3',
2653+
],
2654+
'paths' => new \ArrayObject([
2655+
'/dummies' => [
2656+
'get' => new \ArrayObject([
2657+
'tags' => ['Dummy'],
2658+
'operationId' => 'getDummyCollection',
2659+
'summary' => 'Retrieves the collection of Dummy resources.',
2660+
'parameters' => [
2661+
[
2662+
'name' => 'page',
2663+
'in' => 'query',
2664+
'required' => false,
2665+
'schema' => [
2666+
'type' => 'integer',
2667+
'default' => 1,
2668+
],
2669+
'description' => 'The collection page number',
2670+
],
2671+
[
2672+
'name' => 'itemsPerPage',
2673+
'in' => 'query',
2674+
'required' => false,
2675+
'schema' => [
2676+
'type' => 'integer',
2677+
'default' => 20,
2678+
'minimum' => 0,
2679+
'maximum' => 80,
2680+
],
2681+
'description' => 'The number of items per page',
2682+
],
2683+
],
2684+
'responses' => [
2685+
'200' => [
2686+
'description' => 'Dummy collection response',
2687+
'content' => [
2688+
'application/ld+json' => [
2689+
'schema' => [
2690+
'type' => 'array',
2691+
'items' => ['$ref' => '#/components/schemas/Dummy'],
2692+
],
2693+
],
2694+
],
2695+
],
2696+
],
2697+
]),
2698+
],
2699+
]),
2700+
'components' => [
2701+
'schemas' => new \ArrayObject([
2702+
'Dummy' => new \ArrayObject([
2703+
'type' => 'object',
2704+
'description' => 'This is a dummy.',
2705+
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
2706+
'properties' => [
2707+
'id' => new \ArrayObject([
2708+
'type' => 'integer',
2709+
'description' => 'This is an id.',
2710+
'readOnly' => true,
2711+
]),
2712+
'name' => new \ArrayObject([
2713+
'type' => 'string',
2714+
'description' => 'This is a name.',
2715+
'enum' => ['one', 'two'],
2716+
'example' => 'one',
2717+
]),
2718+
],
2719+
]),
2720+
]),
2721+
],
2722+
];
2723+
2724+
$this->assertEquals($expected, $normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['base_url' => '/app_dev.php/']));
2725+
}
2726+
2727+
/**
2728+
* @group legacy
2729+
* @expectedDeprecation The "maximum_items_per_page" option has been deprecated since API Platform 2.5 in favor of "pagination_maximum_items_per_page" and will be removed in API Platform 3.
2730+
*/
2731+
public function testLegacyNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): void
2732+
{
2733+
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), 'Test API', 'This is a test API.', '1.2.3');
2734+
2735+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
2736+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name']));
2737+
26012738
$dummyMetadata = new ResourceMetadata(
26022739
'Dummy',
26032740
'This is a dummy.',

0 commit comments

Comments
 (0)