Skip to content

Enable the pagination via cursor (no page-based pagination) #2532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2019
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
17 changes: 17 additions & 0 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelatedToDummyFriend as RelatedToDummyFriendDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\RelationEmbedder as RelationEmbedderDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SecuredDummy as SecuredDummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SoMany as SoManyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
Expand Down Expand Up @@ -89,6 +90,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SecuredDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SoMany;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
Expand Down Expand Up @@ -159,6 +161,21 @@ public function thereAreDummyObjects(int $nb)
$this->manager->flush();
}

/**
* @Given there are :nb of these so many objects
*/
public function thereAreOfTheseSoManyObjects(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$dummy = $this->isOrm() ? new SoMany() : new SoManyDocument();
$dummy->content = 'Many #'.$i;

$this->manager->persist($dummy);
}

$this->manager->flush();
}

/**
* @Given there are :nb foo objects with fake names
*/
Expand Down
106 changes: 106 additions & 0 deletions features/hydra/collection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,109 @@ Feature: Collections support
When I send a "GET" request to "/dummies?itemsPerPage=0&page=2"
Then the response status code should be 400
And the JSON node "hydra:description" should be equal to "Page should not be greater than 1 if limit is equal to 0"

Scenario: Cursor-based pagination with an empty collection
When I send a "GET" request to "/so_manies"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/SoMany$"},
"@id": {"pattern": "^/so_manies$"},
"@type": {"pattern": "^hydra:Collection"},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/so_manies$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
},
"additionalProperties": false
},
"hydra:member": {
"type": "array"
}
}
}
"""

@createSchema
Scenario: Cursor-based pagination with ranged items
Given there are 10 of these so many objects
When I send a "GET" request to "/so_manies?order[id]=desc"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/SoMany$"},
"@id": {"pattern": "^/so_manies$"},
"@type": {"pattern": "^hydra:Collection"},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/so_manies\\?order%5Bid%5D=desc$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"},
"hydra:previous": {"pattern": "^/so_manies\\?order%5Bid%5D=desc&id%5Bgt%5D=10$"},
"hydra:next": {"pattern": "^/so_manies\\?order%5Bid%5D=desc&id%5Blt%5D=8$"}
},
"additionalProperties": false
},
"hydra:member": {
"type": "array",
"items": {
"type": "object",
"properties": {
"@id": {
"oneOf": [
{"pattern": "^/so_manies/8$"},
{"pattern": "^/so_manies/9$"},
{"pattern": "^/so_manies/10$"}
]
}
}
},
"minItems": 3
}
}
}
"""

@createSchema
Scenario: Cursor-based pagination with range filtered items
Given there are 10 of these so many objects
When I send a "GET" request to "/so_manies?order[id]=desc&id[gt]=10"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/SoMany$"},
"@id": {"pattern": "^/so_manies$"},
"@type": {"pattern": "^hydra:Collection"},
"hydra:member": {
"type": "array",
"maxItems": 0
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/so_manies\\?order%5Bid%5D=desc&id%5Bgt%5D=10$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"},
"hydra:previous": {"pattern": "^/so_manies\\?order%5Bid%5D=desc&id%5Bgt%5D=13$"},
"hydra:next": {"pattern": "^/so_manies\\?order%5Bid%5D=desc&id%5Blt%5D=10$"}
},
"additionalProperties": false
}
}
}
"""
8 changes: 8 additions & 0 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @Attribute("paginationFetchJoinCollection", type="bool"),
* @Attribute("paginationItemsPerPage", type="int"),
* @Attribute("paginationPartial", type="bool"),
* @Attribute("paginationViaCursor", type="array"),
* @Attribute("routePrefix", type="string"),
* @Attribute("shortName", type="string"),
* @Attribute("subresourceOperations", type="array"),
Expand Down Expand Up @@ -304,6 +305,13 @@ final class ApiResource
*/
private $openapiContext;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var array
*/
private $paginationViaCursor;

/**
* @throws InvalidArgumentException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/hydra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<argument type="service" id="api_platform.hydra.normalizer.partial_collection_view.inner" />
<argument>%api_platform.collection.pagination.page_parameter_name%</argument>
<argument>%api_platform.collection.pagination.enabled_parameter_name%</argument>
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.property_accessor" />
</service>

<service id="api_platform.hydra.normalizer.collection_filters" class="ApiPlatform\Core\Hydra\Serializer\CollectionFiltersNormalizer" decorates="api_platform.hydra.normalizer.collection" public="false">
Expand Down
48 changes: 45 additions & 3 deletions src/Hydra/Serializer/PartialCollectionViewNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

use ApiPlatform\Core\DataProvider\PaginatorInterface;
use ApiPlatform\Core\DataProvider\PartialPaginatorInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Util\IriHelper;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand All @@ -31,12 +34,16 @@ final class PartialCollectionViewNormalizer implements NormalizerInterface, Norm
private $collectionNormalizer;
private $pageParameterName;
private $enabledParameterName;
private $resourceMetadataFactory;
private $propertyAccessor;

public function __construct(NormalizerInterface $collectionNormalizer, string $pageParameterName = 'page', string $enabledParameterName = 'pagination')
public function __construct(NormalizerInterface $collectionNormalizer, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', ResourceMetadataFactoryInterface $resourceMetadataFactory = null, PropertyAccessorInterface $propertyAccessor = null)
{
$this->collectionNormalizer = $collectionNormalizer;
$this->pageParameterName = $pageParameterName;
$this->enabledParameterName = $enabledParameterName;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}

/**
Expand Down Expand Up @@ -69,12 +76,29 @@ public function normalize($object, $format = null, array $context = [])
return $data;
}

$metadata = isset($context['resource_class']) && null !== $this->resourceMetadataFactory ? $this->resourceMetadataFactory->create($context['resource_class']) : null;
$isPaginatedWithCursor = $paginated && null !== $metadata && null !== $cursorPaginationAttribute = $metadata->getCollectionOperationAttribute($context['collection_operation_name'], 'pagination_via_cursor', null, true);

$data['hydra:view'] = [
'@id' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null),
'@id' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated && !$isPaginatedWithCursor ? $currentPage : null),
'@type' => 'hydra:PartialCollectionView',
];

if ($paginated) {
if ($isPaginatedWithCursor) {
$objects = iterator_to_array($object);
$firstObject = current($objects);
$lastObject = end($objects);

$data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters']);

if (false !== $lastObject) {
$data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject)));
}

if (false !== $firstObject) {
$data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, -1, $firstObject)));
}
} elseif ($paginated) {
if (null !== $lastPage) {
$data['hydra:view']['hydra:first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.);
$data['hydra:view']['hydra:last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage);
Expand Down Expand Up @@ -117,4 +141,22 @@ public function setNormalizer(NormalizerInterface $normalizer)
$this->collectionNormalizer->setNormalizer($normalizer);
}
}

private function cursorPaginationFields(array $fields, int $direction, $object)
{
$paginationFilters = [];

foreach ($fields as $field) {
$forwardRangeOperator = 'desc' === strtolower($field['direction']) ? 'lt' : 'gt';
$backwardRangeOperator = 'gt' === $forwardRangeOperator ? 'lt' : 'gt';

$operator = $direction > 0 ? $forwardRangeOperator : $backwardRangeOperator;

$paginationFilters[$field['field']] = [
$operator => (string) $this->propertyAccessor->getValue($object, $field['field']),
];
}

return $paginationFilters;
}
}
4 changes: 2 additions & 2 deletions src/Util/IriHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public static function parseIri(string $iri, string $pageParameterName): array
*
* @param float $page
*/
public static function createIri(array $parts, array $parameters, string $pageParameterName, float $page = null, bool $absoluteUrl = false): string
public static function createIri(array $parts, array $parameters, string $pageParameterName = null, float $page = null, bool $absoluteUrl = false): string
{
if (null !== $page) {
if (null !== $page && null !== $pageParameterName) {
$parameters[$pageParameterName] = $page;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Fixtures/TestBundle/Document/SoMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\RangeFilter;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document
* @ApiResource(attributes={
* "pagination_partial"=true,
* "pagination_via_cursor"={
* {"field"="id", "direction"="DESC"}
* }
* })
*
* @ApiFilter(RangeFilter::class, properties={"id"})
* @ApiFilter(OrderFilter::class, properties={"id"="DESC"})
*/
class SoMany
{
/**
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
public $id;

/**
* @ODM\Field(nullable=true)
*/
public $content;
}
47 changes: 47 additions & 0 deletions tests/Fixtures/TestBundle/Entity/SoMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ApiResource(attributes={
* "pagination_partial"=true,
* "pagination_via_cursor"={
* {"field"="id", "direction"="DESC"}
* }
* })
*
* @ApiFilter(RangeFilter::class, properties={"id"})
* @ApiFilter(OrderFilter::class, properties={"id"="DESC"})
*/
class SoMany
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @ORM\Column(nullable=true)
*/
public $content;
}
Loading