Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jsonapi): add support for pagination array parameter name #6319

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/JsonApi/State/JsonApiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
if (
\is_array($pageParameter)
) {
$filters = array_merge($pageParameter, $filters);
// To not break existing integration, put page array in _page
$filters = array_merge(['_page' => $pageParameter], $pageParameter, $filters);
}

[$included, $properties] = $this->transformFieldsetsParameters($queryParameters, $operation->getShortName() ?? '');
Expand Down
29 changes: 27 additions & 2 deletions src/State/Pagination/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,38 @@ private function getGraphQlEnabled(?Operation $operation): bool
return $operation?->getPaginationEnabled() ?? $enabled;
}

/**
* Extract pagination parameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pagination parameter value?

* page[page] => $contextFilters['page'] with default configuration page_parameter_name: page
* page[number] => $contextFilters['_page'][number] with configuration page_parameter_name: page[number].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean that you configure page[number] on the page_parameter_name, then you use page[number]=1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add more context to this, JSON:API consider query parameter such as two parameters named page[offset] and page[limit] really are two separate parameters, instead of a single page parameter in an array form.

https://jsonapi.org/format/#query-parameters-families

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah or page[number] & page[size] in my implementation if you work with page numbers instead of offset/limit cf https://jsonapi.org/format/#fetching-pagination

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a function test and a unit test?

*/
private function extractParameter(array $contextFilters, string $parameterName): mixed
{
preg_match_all("/[\w-]+/", $parameterName, $matches);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the need of this regex here, we know the possible parameter names no? I'd love a test to understand this better.

foreach ($matches[0] as $i => $key) {
if (0 === $i && 'page' === $key && \count($matches[0]) > 1) {
$key = '_page';
}
if (false === \is_array($contextFilters)) {
return $contextFilters;
}
if (!\array_key_exists($key, $contextFilters)) {
return null;
}
$contextFilters = $contextFilters[$key];
}

return $contextFilters;
}

/**
* Gets the given pagination parameter name from the given context.
*/
private function getParameterFromContext(array $context, string $parameterName, mixed $default = null)
private function getParameterFromContext(array $context, string $parameterName, mixed $default = null): mixed
{
$filters = $context['filters'] ?? [];
$filterValue = $this->extractParameter($filters, $parameterName);

return \array_key_exists($parameterName, $filters) ? $filters[$parameterName] : $default;
return $filterValue ?? $default;
}
}
66 changes: 66 additions & 0 deletions src/State/Tests/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\State\Tests;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\State\Pagination\Pagination;
use PHPUnit\Framework\TestCase;

class PaginationTest extends TestCase
{
public function testPaginationGetPaginationWithDefaultOptionsAndDefaultContext(): void
{
$operation = new GetCollection(name: 'hello', provider: 'provider');
$pagination = new Pagination();
$paginationInfo = $pagination->getPagination($operation);
$this->assertSame(1, $paginationInfo[0]);
$this->assertSame(0, $paginationInfo[1]);
$this->assertSame(30, $paginationInfo[2]);
}

public function testPaginationGetPaginationWithPageParameterNameAsArrayAndDefaultContext(): void
{
$operation = new GetCollection(name: 'hello', provider: 'provider');
$pagination = new Pagination(['page_parameter_name' => 'page[number]']);
$paginationInfo = $pagination->getPagination($operation);
$this->assertSame(1, $paginationInfo[0]);
$this->assertSame(0, $paginationInfo[1]);
$this->assertSame(30, $paginationInfo[2]);
}

public function testPaginationGetPaginationWithPageParametersAsArrayAndCustomContext(): void
{
$operation = new GetCollection(paginationClientItemsPerPage: true, name: 'hello', provider: 'provider');
$pagination = new Pagination([
'page_parameter_name' => 'page[number]',
'items_per_page_parameter_name' => 'page[size]',
]);
$paginationInfo = $pagination->getPagination(
$operation,
[
'filters' => [
'number' => 2,
'size' => 10,
'_page' => [
'number' => 2,
'size' => 10,
],
],
],
);
$this->assertSame(2, $paginationInfo[0]);
$this->assertSame(10, $paginationInfo[1]);
$this->assertSame(10, $paginationInfo[2]);
}
}