Skip to content

Commit

Permalink
PERF-1343 Fix cursor listing merge error
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyLV committed Feb 11, 2025
1 parent 675d5c6 commit 61e3353
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 6 additions & 4 deletions Api/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -208,6 +208,8 @@ protected function requestAllUsingCursor(
$cursor = $result->getNextCursor();
}

$result->body[$bodyResponseKey] = $bodyData;

return $result;
}

Expand Down
17 changes: 13 additions & 4 deletions Tests/Endpoints/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
Expand All @@ -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)
),
])
),
]);
Expand Down Expand Up @@ -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)
),
])
),
Expand All @@ -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());
}
Expand Down

0 comments on commit 61e3353

Please sign in to comment.