Skip to content

Commit f4e5df0

Browse files
committed
feat(serializer): remove pagination attributes when explicitly disabled
1 parent 07d0ef8 commit f4e5df0

File tree

6 files changed

+383
-0
lines changed

6 files changed

+383
-0
lines changed

src/Hal/JsonSchema/SchemaFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ public function buildSchema(string $className, string $format = 'jsonhal', strin
177177
];
178178
}
179179

180+
if (false === $operation->getPaginationEnabled()) {
181+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['itemsPerPage']);
182+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['_links']['properties']['first']);
183+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['_links']['properties']['last']);
184+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['_links']['properties']['next']);
185+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['_links']['properties']['previous']);
186+
}
187+
180188
unset($schema['items']);
181189
unset($schema['type']);
182190

src/Hydra/JsonSchema/SchemaFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ public function buildSchema(string $className, string $format = 'jsonld', string
235235
];
236236
}
237237

238+
if (false === $operation->getPaginationEnabled()) {
239+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties'][$hydraPrefix.'view']);
240+
}
241+
238242
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
239243
$schema['type'] = 'object';
240244
$schema['description'] = "$definitionName collection.";

src/Hydra/Serializer/PartialCollectionViewNormalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
9595

9696
$partialCollectionView = $this->getPartialCollectionView($object, $context['uri'] ?? $context['request_uri'] ?? '/', $this->pageParameterName, $this->enabledParameterName, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
9797

98+
if (false === $operation?->getPaginationEnabled()) {
99+
return $data;
100+
}
101+
98102
$view = [
99103
'@id' => $partialCollectionView->id,
100104
'@type' => $hydraPrefix.'PartialCollectionView',

src/JsonApi/JsonSchema/SchemaFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ public function buildSchema(string $className, string $format = 'jsonapi', strin
231231
];
232232
}
233233

234+
if (false === $operation->getPaginationEnabled()) {
235+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['properties']['first']);
236+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['properties']['last']);
237+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['properties']['next']);
238+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['properties']['prev']);
239+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['example']['first']);
240+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['example']['last']);
241+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['example']['next']);
242+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['links']['example']['prev']);
243+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['meta']['properties']['itemsPerPage']);
244+
unset($definitions[self::COLLECTION_BASE_SCHEMA_NAME]['properties']['meta']['properties']['currentPage']);
245+
}
246+
234247
unset($schema['items']);
235248
unset($schema['type']);
236249

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
#[ApiResource]
21+
#[GetCollection(
22+
paginationEnabled: false,
23+
paginationItemsPerPage: 3,
24+
)]
25+
#[ORM\Entity]
26+
class PaginationDisabledEntity
27+
{
28+
#[ORM\Column(type: 'integer', nullable: true)]
29+
#[ORM\Id]
30+
#[ORM\GeneratedValue(strategy: 'AUTO')]
31+
private ?int $id = null;
32+
33+
public function getId(): ?int
34+
{
35+
return $this->id;
36+
}
37+
}

0 commit comments

Comments
 (0)