Skip to content
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

fix: remove hydra:next when no items would be returned in the next cursor pagination page #6477

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/Hydra/Serializer/PartialCollectionViewNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
$data['hydra:view'] = ['@id' => null, '@type' => 'hydra:PartialCollectionView'];

if ($isPaginatedWithCursor) {
return $this->populateDataWithCursorBasedPagination($data, $parsed, $object, $cursorPaginationAttribute, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
return $this->populateDataWithCursorBasedPagination($data, $parsed, $object, $cursorPaginationAttribute, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy, $itemsPerPage, $pageTotalItems);
}

$data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated ? $currentPage : null, $operation?->getUrlGenerationStrategy() ?? $this->urlGenerationStrategy);
Expand Down Expand Up @@ -168,15 +168,15 @@ private function cursorPaginationFields(array $fields, int $direction, $object):
return $paginationFilters;
}

private function populateDataWithCursorBasedPagination(array $data, array $parsed, \Traversable $object, ?array $cursorPaginationAttribute, ?int $urlGenerationStrategy): array
private function populateDataWithCursorBasedPagination(array $data, array $parsed, \Traversable $object, ?array $cursorPaginationAttribute, ?int $urlGenerationStrategy, ?float $itemsPerPage, ?float $pageTotalItems): array
{
$objects = iterator_to_array($object);
$firstObject = current($objects);
$lastObject = end($objects);

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

if (false !== $lastObject && \is_array($cursorPaginationAttribute)) {
if (false !== $lastObject && \is_array($cursorPaginationAttribute) && $pageTotalItems >= $itemsPerPage) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when $pageTotalItems == $itemsPerPage we want to show hydra:view? Can you test this? I thought that with a cursor we did not know the amount of total items (no count)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did test it out, we have it live in our production environment. This isn't perfect because it still could return no results on the next page, however, it prevents an empty page from showing hydra:next.

The logic is that with cursor pagination if you no longer have results on a page, the next one won't either because you are doing a less less than or greater than some identifier. Because it's only a comparison like that if it doesn't return enough results we can be certain the next page will return none.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you be able to add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I haven't gotten around to adding a test here. It isn't forgotten though.

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

Expand Down
Loading