From 84e12d61e64ee19152f2d99e5a1a981ff683c414 Mon Sep 17 00:00:00 2001 From: Alexander Kim Date: Thu, 18 Jul 2024 14:17:36 -0400 Subject: [PATCH] fix: remove hydra:next when no items would be returned in the next cursor pagination page --- src/Hydra/Serializer/PartialCollectionViewNormalizer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Hydra/Serializer/PartialCollectionViewNormalizer.php b/src/Hydra/Serializer/PartialCollectionViewNormalizer.php index 95fb34245d2..8ff3786e3cd 100644 --- a/src/Hydra/Serializer/PartialCollectionViewNormalizer.php +++ b/src/Hydra/Serializer/PartialCollectionViewNormalizer.php @@ -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); @@ -168,7 +168,7 @@ 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); @@ -176,7 +176,7 @@ private function populateDataWithCursorBasedPagination(array $data, array $parse $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) { $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject)), urlGenerationStrategy: $urlGenerationStrategy); }