fix(hydra): emit hydra:next and hydra:previous on empty cursor-paginated collections#7961
Open
guillaumedelre wants to merge 3 commits intoapi-platform:4.3from
Open
fix(hydra): emit hydra:next and hydra:previous on empty cursor-paginated collections#7961guillaumedelre wants to merge 3 commits intoapi-platform:4.3from
guillaumedelre wants to merge 3 commits intoapi-platform:4.3from
Conversation
Author
|
Hey @soyuka @dunglas @alanpoulain, this PR fixes #7953 — happy to adjust if anything looks off. |
9458d9c to
6244b35
Compare
Member
|
can you target 4.3? The issue in behat is because of the wrong use of filters I'll try to take a look shortly |
Author
|
@soyuka i have just changed the target to 4.3 |
…ted collections When a cursor-paginated collection returns no items and the request URL already contains a cursor filter (e.g. id[gt]=10), navigation links were silently omitted because populateDataWithCursorBasedPagination() relies on reading the first and last objects of the collection. Synthesize the links from the URL parameters instead: - hydra:next: invert the cursor operator (gt -> lt), keep the value - hydra:previous: keep the operator, shift the value by items_per_page If no cursor filter is present in the URL, the behavior is unchanged. Fixes api-platform#7953 Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com>
6244b35 to
ad606ca
Compare
| Q | A | ------------- | --- | Branch? | 4.3 | Tickets | refs api-platform#7953 | License | MIT | Doc PR | ∅ Replace #[ApiFilter(RangeFilter)] / #[ApiFilter(OrderFilter)] with QueryParameter + ComparisonFilter(ExactFilter) + SortFilter, so the new regression fixture does not ship usages slated for deprecation in 4.4 and removal in 6.0 per TODO-filters.md.
| Q | A | ------------- | --- | Branch? | 4.3 | Tickets | refs api-platform#7953 | License | MIT | Doc PR | ∅ Entity\Dummy and Entity\Issue7953\Dummy both auto-derive table name "Dummy", so SchemaTool::createSchema throws TableAlreadyExists and cascades 500s through every later behat scenario.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's in this PR?
When a cursor-paginated collection returns no items (e.g. a range filter excludes all rows),
hydra:viewwas emitted with only@idand@type. Thehydra:nextandhydra:previouslinks were silently dropped, leaving clients with no way to navigate without out-of-band knowledge of the cursor field and items-per-page value.This was a regression from pre-4.x behavior, verified by the Behat scenario "Cursor-based pagination with range filtered items" in
features/hydra/collection.feature.Root cause
PartialCollectionViewNormalizer::populateDataWithCursorBasedPagination()builds navigation links by reading the first and last objects of the collection. When the collection is empty, bothcurrent()andend()returnfalseand both branches are skipped.Fix
When the collection is empty and the request URL already contains a cursor filter parameter for a field declared in
paginationViaCursor, synthesize the navigation links directly from the URL parameters:hydra:next: invert the cursor operator (gt→lt), keep the valuehydra:previous: keep the operator, shift the value byitems_per_pageIf no cursor filter is present in the URL (e.g. a plain request to
/resourcesthat returns nothing), the behavior is unchanged: no links are emitted.Tests
PartialCollectionViewNormalizerTest::testNormalizeWithCursorBasedPaginationEmptyCollection— unit test, synthesized URLs verified against expected valuesCursorPaginationEmptyCollectionTest::testEmptyCollectionWithCursorFilterHasNavigationLinks— functional test with Doctrine/SQLite, 10 fixtures inserted, empty result forced viaid[gt]=10Full Hydra component suite: 51 tests / 202 assertions, all green (3 PHPUnit notices are pre-existing).