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

Conversation

seb37800
Copy link

@seb37800 seb37800 commented Apr 16, 2024

Q A
Branch? 3.2
Tickets
License MIT
Doc PR

This bugfix is related to the jsonapi spec & the handling of pagination parameters in the pagination service.
Following the JSON:API specification around pagination => https://jsonapi.org/format/#fetching-pagination
When i setup api_platform.yaml with

api_platform:
    collection:
        pagination:
            page_parameter_name: page[number]
            items_per_page_parameter_name: page[size]

And use ApiPlatform\State\Pagination\Pagination in a custom provider, we can't retrieve the page[number] & page[size] in $context['filters'].

Thanks to @GregoireHebert for the help.

In addition this fix that let us specify array in pagination parameter names will fix the generated documentation.

@seb37800 seb37800 marked this pull request as draft April 16, 2024 09:46
@seb37800 seb37800 marked this pull request as ready for review April 16, 2024 13:18
* page[page] => $contextFilters['page'] with default configuration page_parameter_name: page
* page[number] => $contextFilters['_page'][number] with configuration page_parameter_name: page[number].
*/
private function extractParameter(array $contextFilters, string $parameterName)
Copy link
Member

Choose a reason for hiding this comment

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

missing return type

Copy link
Author

Choose a reason for hiding this comment

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

i've added mixed, but not very top

/**
* Extract pagination parameter
* 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?

/**
* Extract pagination parameter
* 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.

can you add a function test and a unit test?

@@ -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?

*/
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.

@seb37800 seb37800 requested a review from soyuka May 13, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants