Skip to content

Commit 98899c6

Browse files
committed
Merge branch 'release/4.3.1'
2 parents 05644f3 + 8d96752 commit 98899c6

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
## [4.3.1] - 2024-10-31
9+
10+
### Fixed
11+
12+
- [#41](https://github.com/laravel-json-api/eloquent/pull/41) Handle key column not existing in cursor paginator.
13+
814
## [4.3.0] - 2024-10-13
915

1016
### Added

src/Pagination/Cursor/CursorParser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ public function __construct(private IdParser $idParser, private string $keyName)
3232
*/
3333
public function encode(LaravelCursor $cursor): string
3434
{
35-
$key = $cursor->parameter($this->keyName);
36-
37-
if ($key) {
35+
try {
36+
$key = $cursor->parameter($this->keyName);
3837
$parameters = $this->withoutPrivate($cursor->toArray());
3938
$parameters[$this->keyName] = $this->idParser->encode($key);
4039
$cursor = new LaravelCursor($parameters, $cursor->pointsToNextItems());
40+
} catch (\UnexpectedValueException $ex) {
41+
// Do nothing as the cursor does not contain the key.
4142
}
4243

4344
return $cursor->encode();

tests/lib/Acceptance/Pagination/CursorPaginationTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,57 @@ public function testWithoutCursor(): void
275275
$this->assertPage($posts->reverse()->take(3), $page);
276276
}
277277

278+
/**
279+
* @return void
280+
*/
281+
public function testWithIdEncodingWithoutKeySort(): void
282+
{
283+
$this->withIdEncoding();
284+
$this->paginator->withoutKeySort();
285+
286+
$posts = Post::factory()->count(4)->sequence(['title' => 'd'], ['title' => 'c'], ['title' => 'b'], ['title' => 'a'])->create();
287+
288+
$meta = [
289+
'from' => $this->encodeCursor(
290+
["posts.title"=> "a"],
291+
pointsToNextItems: false,
292+
),
293+
'hasMore' => true,
294+
'perPage' => 3,
295+
'to' => $this->encodeCursor(
296+
["posts.title"=> "c"],
297+
pointsToNextItems: true,
298+
),
299+
];
300+
301+
$links = [
302+
'first' => [
303+
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
304+
'page' => ['limit' => '3'],
305+
'sort' => 'title',
306+
]),
307+
],
308+
'next' => [
309+
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
310+
'page' => [
311+
'after' => $this->encodeCursor(
312+
["posts.title"=> "c"],
313+
pointsToNextItems: true,
314+
),
315+
'limit' => '3',
316+
],
317+
'sort' => 'title',
318+
]),
319+
],
320+
];
321+
322+
$page = $this->posts->repository()->queryAll()->sort('title')->paginate(['limit' => '3']);
323+
324+
$this->assertSame(['page' => $meta], $page->meta());
325+
$this->assertSame($links, $page->links()->toArray());
326+
$this->assertPage($posts->reverse()->take(3), $page);
327+
}
328+
278329
/**
279330
* @return void
280331
*/

0 commit comments

Comments
 (0)