From 53c8c2d9fe5f0d02e1de3632bf63aeac68db1da0 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 27 Nov 2026 00:15:44 -0400 Subject: [PATCH 1/3] fix: use the new cursor and page size when fetching the next page Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 4 ++ lib/EasyPost/Service/BaseService.php | 10 ++-- lib/EasyPost/Service/UserService.php | 10 ++-- test/EasyPost/PaginationTest.php | 84 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 test/EasyPost/PaginationTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b8a04366..922d6f8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Next Release + +- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size` + ## v8.10.0 (2026-11-09) - Adds `delivery_evidence` to `Tracker` and a `DeliveryEvidence` model diff --git a/lib/EasyPost/Service/BaseService.php b/lib/EasyPost/Service/BaseService.php index eace7b91..6567c047 100644 --- a/lib/EasyPost/Service/BaseService.php +++ b/lib/EasyPost/Service/BaseService.php @@ -163,13 +163,13 @@ protected function getNextPageResources(string $class, mixed $collection, ?int $ throw new EndOfPaginationException(); } - $params = [ - 'page_size' => $pageSize, + // The new cursor (and page size, if given) must win over the params of the previous request + $params = array_merge($userParams ?? [], [ 'before_id' => $collectionArray[count($collectionArray) - 1]['id'] - ]; + ]); - if (isset($userParams)) { - $params = array_merge($params, $userParams); + if (isset($pageSize)) { + $params['page_size'] = $pageSize; } $response = $this->allResources($class, $params); diff --git a/lib/EasyPost/Service/UserService.php b/lib/EasyPost/Service/UserService.php index 21a622bf..b442927d 100644 --- a/lib/EasyPost/Service/UserService.php +++ b/lib/EasyPost/Service/UserService.php @@ -127,13 +127,13 @@ public function getNextPageOfChildren(mixed $children, ?int $pageSize = null): m throw new EndOfPaginationException(); } - $params = [ - 'page_size' => $pageSize, + // The new cursor (and page size, if given) must win over the params of the previous request + $params = array_merge($userParams ?? [], [ 'after_id' => $userArray[count($userArray) - 1]['id'] - ]; + ]); - if (isset($userParams)) { - $params = array_merge($params, $userParams); + if (isset($pageSize)) { + $params['page_size'] = $pageSize; } // Retrieve a page of users from the server diff --git a/test/EasyPost/PaginationTest.php b/test/EasyPost/PaginationTest.php new file mode 100644 index 00000000..852fb932 --- /dev/null +++ b/test/EasyPost/PaginationTest.php @@ -0,0 +1,84 @@ + */ + private array $sentParams = []; + + /** + * Build a mocked client that records the query params of every request. + */ + private function client(string $urlPattern, string $responseBody): EasyPostClient + { + $client = new EasyPostClient('test_key', 60.0, 'https://api.easypost.com', new MockingUtility([ + new MockRequest( + new MockRequestMatchRule('get', $urlPattern), + new MockRequestResponseInfo(200, $responseBody) + ), + ])); + $client->subscribeToRequestHook(function (array $args): void { + $this->sentParams[] = $args['request_body']; + }); + + return $client; + } + + /** + * Test that the new cursor and page size win over the params of the previous request. + */ + public function testGetNextPageCursorOverridesPreviousParams(): void + { + $client = $this->client('/v2\\/shipments$/', '{"shipments": [{"id": "shp_last"}], "has_more": true}'); + + $shipments = $client->shipment->all(['before_id' => 'shp_old', 'page_size' => 5, 'purchased' => false]); + $nextPage = $client->shipment->getNextPage($shipments, 2); + + $this->assertEquals('shp_last', $this->sentParams[1]['before_id']); + $this->assertEquals(2, $this->sentParams[1]['page_size']); + $this->assertFalse($this->sentParams[1]['purchased']); + + $client->shipment->getNextPage($nextPage); + + $this->assertEquals('shp_last', $this->sentParams[2]['before_id']); + $this->assertEquals(5, $this->sentParams[2]['page_size']); + } + + /** + * Test that the new cursor wins over the params of the previous request for child users. + */ + public function testGetNextPageOfChildrenCursorOverridesPreviousParams(): void + { + $client = $this->client('/v2\\/users\\/children$/', '{"children": [{"id": "user_last"}], "has_more": true}'); + + $children = $client->user->allChildren(['after_id' => 'user_old', 'page_size' => 5]); + $client->user->getNextPageOfChildren($children, 2); + + $this->assertEquals('user_last', $this->sentParams[1]['after_id']); + $this->assertEquals(2, $this->sentParams[1]['page_size']); + } + + /** + * Test that the cursor keeps advancing when the first request had no params. + */ + public function testGetNextPageAdvancesWithoutInitialParams(): void + { + $client = $this->client('/v2\\/shipments$/', '{"shipments": [{"id": "shp_last"}], "has_more": true}'); + + $firstPage = $client->shipment->all(); + $secondPage = $client->shipment->getNextPage($firstPage); + $lastShipment = $secondPage['shipments'][0]; + $lastShipment->id = 'shp_second'; + $client->shipment->getNextPage($secondPage); + + $this->assertEquals('shp_second', $this->sentParams[2]['before_id']); + } +} From c5357c6351ffc0576fdb2cefe83d6f78645d21a4 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 30 Nov 2026 09:16:20 -0700 Subject: [PATCH 2/3] fix: keep date filters when paginating reports --- CHANGELOG.md | 3 ++- lib/EasyPost/Service/ReportService.php | 11 ++++++----- test/EasyPost/PaginationTest.php | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 922d6f8e..d72c748f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## Next Release -- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size` +- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size` (thanks @pcbeingused33) +- Fixes `report->getNextPage` dropping the date filters of the original `all` call ## v8.10.0 (2026-11-09) diff --git a/lib/EasyPost/Service/ReportService.php b/lib/EasyPost/Service/ReportService.php index 8093376a..20dd0ef4 100644 --- a/lib/EasyPost/Service/ReportService.php +++ b/lib/EasyPost/Service/ReportService.php @@ -43,6 +43,7 @@ public function all(mixed $params = null): mixed unset($params['type']); $response = Requestor::request($this->client, HttpMethod::GET, $url, $params); $response['type'] = $type; + $response['_params'] = $params; return InternalUtil::convertToEasyPostObject($this->client, $response); } @@ -64,13 +65,13 @@ public function getNextPage(mixed $reports, ?int $pageSize = null): mixed throw new EndOfPaginationException(); } - $params = [ - 'page_size' => $pageSize, + // The new cursor (and page size, if given) must win over the params of the previous request + $params = array_merge($userParams ?? [], [ 'before_id' => $reportArray[count($reportArray) - 1]['id'] - ]; + ]); - if (isset($userParams)) { - $params = array_merge($params, $userParams); + if (isset($pageSize)) { + $params['page_size'] = $pageSize; } $url = self::reportUrl($reportArray[0]->object); diff --git a/test/EasyPost/PaginationTest.php b/test/EasyPost/PaginationTest.php index 852fb932..e7c8c3b2 100644 --- a/test/EasyPost/PaginationTest.php +++ b/test/EasyPost/PaginationTest.php @@ -81,4 +81,26 @@ public function testGetNextPageAdvancesWithoutInitialParams(): void $this->assertEquals('shp_second', $this->sentParams[2]['before_id']); } + + /** + * Test that the date filters of the first request carry over to the next page of reports. + */ + public function testGetNextPageOfReportsKeepsDateFilters(): void + { + $client = $this->client( + '/v2\\/reports\\/shipment$/', + '{"reports": [{"id": "shprep_last", "object": "ShipmentReport"}], "has_more": true}' + ); + + $reports = $client->report->all([ + 'type' => 'shipment', + 'start_datetime' => '2022-01-01', + 'end_datetime' => '2022-02-01', + ]); + $client->report->getNextPage($reports); + + $this->assertEquals('shprep_last', $this->sentParams[1]['before_id']); + $this->assertEquals('2022-01-01', $this->sentParams[1]['start_datetime']); + $this->assertEquals('2022-02-01', $this->sentParams[1]['end_datetime']); + } } From f3af74b8e7be37df342b508387d862af0f50681c Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 30 Nov 2026 09:43:35 -0700 Subject: [PATCH 3/3] fix: drop the other cursor and report date claim on next pages --- CHANGELOG.md | 3 +-- lib/EasyPost/Service/BaseService.php | 1 + lib/EasyPost/Service/ReportService.php | 1 + lib/EasyPost/Service/UserService.php | 1 + test/EasyPost/PaginationTest.php | 16 ++++++++-------- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d72c748f..204732c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,7 @@ ## Next Release -- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size` (thanks @pcbeingused33) -- Fixes `report->getNextPage` dropping the date filters of the original `all` call +- Fixes `getNextPage` and `getNextPageOfChildren` requesting the same page again when the previous request params already contained a cursor, and ignoring the `pageSize` argument when the previous params had a `page_size`, and sending both `before_id` and `after_id` when the previous params already contained the other cursor (thanks @pcbeingused33) ## v8.10.0 (2026-11-09) diff --git a/lib/EasyPost/Service/BaseService.php b/lib/EasyPost/Service/BaseService.php index 6567c047..01a4aab7 100644 --- a/lib/EasyPost/Service/BaseService.php +++ b/lib/EasyPost/Service/BaseService.php @@ -167,6 +167,7 @@ protected function getNextPageResources(string $class, mixed $collection, ?int $ $params = array_merge($userParams ?? [], [ 'before_id' => $collectionArray[count($collectionArray) - 1]['id'] ]); + unset($params['after_id']); if (isset($pageSize)) { $params['page_size'] = $pageSize; diff --git a/lib/EasyPost/Service/ReportService.php b/lib/EasyPost/Service/ReportService.php index 20dd0ef4..f1fcceaf 100644 --- a/lib/EasyPost/Service/ReportService.php +++ b/lib/EasyPost/Service/ReportService.php @@ -69,6 +69,7 @@ public function getNextPage(mixed $reports, ?int $pageSize = null): mixed $params = array_merge($userParams ?? [], [ 'before_id' => $reportArray[count($reportArray) - 1]['id'] ]); + unset($params['after_id']); if (isset($pageSize)) { $params['page_size'] = $pageSize; diff --git a/lib/EasyPost/Service/UserService.php b/lib/EasyPost/Service/UserService.php index b442927d..7e946779 100644 --- a/lib/EasyPost/Service/UserService.php +++ b/lib/EasyPost/Service/UserService.php @@ -131,6 +131,7 @@ public function getNextPageOfChildren(mixed $children, ?int $pageSize = null): m $params = array_merge($userParams ?? [], [ 'after_id' => $userArray[count($userArray) - 1]['id'] ]); + unset($params['before_id']); if (isset($pageSize)) { $params['page_size'] = $pageSize; diff --git a/test/EasyPost/PaginationTest.php b/test/EasyPost/PaginationTest.php index e7c8c3b2..e14c0a03 100644 --- a/test/EasyPost/PaginationTest.php +++ b/test/EasyPost/PaginationTest.php @@ -39,10 +39,11 @@ public function testGetNextPageCursorOverridesPreviousParams(): void { $client = $this->client('/v2\\/shipments$/', '{"shipments": [{"id": "shp_last"}], "has_more": true}'); - $shipments = $client->shipment->all(['before_id' => 'shp_old', 'page_size' => 5, 'purchased' => false]); + $shipments = $client->shipment->all(['after_id' => 'shp_old', 'page_size' => 5, 'purchased' => false]); $nextPage = $client->shipment->getNextPage($shipments, 2); $this->assertEquals('shp_last', $this->sentParams[1]['before_id']); + $this->assertArrayNotHasKey('after_id', $this->sentParams[1]); $this->assertEquals(2, $this->sentParams[1]['page_size']); $this->assertFalse($this->sentParams[1]['purchased']); @@ -59,10 +60,11 @@ public function testGetNextPageOfChildrenCursorOverridesPreviousParams(): void { $client = $this->client('/v2\\/users\\/children$/', '{"children": [{"id": "user_last"}], "has_more": true}'); - $children = $client->user->allChildren(['after_id' => 'user_old', 'page_size' => 5]); + $children = $client->user->allChildren(['before_id' => 'user_old', 'page_size' => 5]); $client->user->getNextPageOfChildren($children, 2); $this->assertEquals('user_last', $this->sentParams[1]['after_id']); + $this->assertArrayNotHasKey('before_id', $this->sentParams[1]); $this->assertEquals(2, $this->sentParams[1]['page_size']); } @@ -83,9 +85,9 @@ public function testGetNextPageAdvancesWithoutInitialParams(): void } /** - * Test that the date filters of the first request carry over to the next page of reports. + * Test that the params of the first request carry over to the next page of reports. */ - public function testGetNextPageOfReportsKeepsDateFilters(): void + public function testGetNextPageOfReportsKeepsPreviousParams(): void { $client = $this->client( '/v2\\/reports\\/shipment$/', @@ -94,13 +96,11 @@ public function testGetNextPageOfReportsKeepsDateFilters(): void $reports = $client->report->all([ 'type' => 'shipment', - 'start_datetime' => '2022-01-01', - 'end_datetime' => '2022-02-01', + 'page_size' => 5, ]); $client->report->getNextPage($reports); $this->assertEquals('shprep_last', $this->sentParams[1]['before_id']); - $this->assertEquals('2022-01-01', $this->sentParams[1]['start_datetime']); - $this->assertEquals('2022-02-01', $this->sentParams[1]['end_datetime']); + $this->assertEquals(5, $this->sentParams[1]['page_size']); } }