From 61e3353c7f86df0754cfe3b6f70ab5600d532758 Mon Sep 17 00:00:00 2001 From: RonnyLV Date: Tue, 11 Feb 2025 00:20:51 +0200 Subject: [PATCH] PERF-1343 Fix cursor listing merge error --- Api/Endpoints/Endpoint.php | 10 ++++++---- Tests/Endpoints/EndpointTest.php | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Api/Endpoints/Endpoint.php b/Api/Endpoints/Endpoint.php index eb7269c..16b84d8 100644 --- a/Api/Endpoints/Endpoint.php +++ b/Api/Endpoints/Endpoint.php @@ -147,7 +147,7 @@ protected function requestAllUsingPaging( $bodyData = []; $result = $this->request($requestType, $uri, $queryParams, $body); - if (isset($result->body[$bodyResponseKey]) && is_array($result->body[$bodyResponseKey])) { + if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) { $bodyData = $result->body[$bodyResponseKey]; } while ($result->getPageCount() > $page) { @@ -157,12 +157,13 @@ protected function requestAllUsingPaging( $queryParams['page'] = $page; $result = $this->request($requestType, $uri, $queryParams, $body); - if (is_array($result->body[$bodyResponseKey])) { + if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) { $bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]); - $result->body[$bodyResponseKey] = $bodyData; } } + $result->body[$bodyResponseKey] = $bodyData; + return $result; } @@ -198,7 +199,6 @@ protected function requestAllUsingCursor( if (is_array($result->body[$bodyResponseKey]) && !empty($result->body[$bodyResponseKey])) { $bodyData = array_merge($bodyData, $result->body[$bodyResponseKey]); - $result->body[$bodyResponseKey] = $bodyData; } if (!$result->hasNextCursor()) { @@ -208,6 +208,8 @@ protected function requestAllUsingCursor( $cursor = $result->getNextCursor(); } + $result->body[$bodyResponseKey] = $bodyData; + return $result; } diff --git a/Tests/Endpoints/EndpointTest.php b/Tests/Endpoints/EndpointTest.php index 9441e32..84729cf 100644 --- a/Tests/Endpoints/EndpointTest.php +++ b/Tests/Endpoints/EndpointTest.php @@ -204,7 +204,7 @@ public function fetchAll(): ApiResponse "X-Pagination-Total-Count" => 14, "X-Pagination-Limit" => 10, "X-Pagination-Page" => 1, - "X-Pagination-Page-Count" => 2, + "X-Pagination-Page-Count" => 3, ], json_encode(['mockedResults' => [["id" => "element from page 1 using paging"]]], JSON_THROW_ON_ERROR) ), @@ -214,10 +214,20 @@ public function fetchAll(): ApiResponse "X-Pagination-Total-Count" => 14, "X-Pagination-Limit" => 10, "X-Pagination-Page" => 2, - "X-Pagination-Page-Count" => 2, + "X-Pagination-Page-Count" => 3, ], json_encode(['mockedResults' => [["id" => "element from page 2 using paging"]]], JSON_THROW_ON_ERROR) ), + new Response( + 200, + [ + "X-Pagination-Total-Count" => 14, + "X-Pagination-Limit" => 10, + "X-Pagination-Page" => 3, + "X-Pagination-Page-Count" => 3, + ], + json_encode(['mockedResults' => []], JSON_THROW_ON_ERROR) + ), ]) ), ]); @@ -267,7 +277,7 @@ public function fetchAll(): ApiResponse "X-Pagination-Next-Cursor" => "", "X-Pagination-Limit" => 10, ], - json_encode(['mockedResults' => [["id" => "element from cursor 3 using cursor"]]], JSON_THROW_ON_ERROR) + json_encode(['mockedResults' => []], JSON_THROW_ON_ERROR) ), ]) ), @@ -281,7 +291,6 @@ public function fetchAll(): ApiResponse "mockedResults" => [ ["id" => "element from cursor 1 using cursor"], ["id" => "element from cursor 2 using cursor"], - ["id" => "element from cursor 3 using cursor"], ] ], $apiResponse->getContent()); }