Skip to content

Throw 400 status erro when a non numeric value is encountered on page… #1872

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/jsonapi/pagination.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ Feature: JSON API pagination handling
And the JSON node "meta.totalItems" should be equal to the number 10
And the JSON node "meta.itemsPerPage" should be equal to the number 15
And the JSON node "meta.currentPage" should be equal to the number 1

Scenario: Get a paginated collection with an non numeric page request attribute
When I send a "GET" request to "/dummies?page[page]=dummie_non_numeric"
Then the response should be in JSON
And the response status code should be 400
And the JSON node title should be equal to 'An error occurred'
And the JSON node description should be equal to 'Page request attribute must be a numeric value equal or greater than 1'

Scenario: Get a paginated collection with a value lower than 1
When I send a "GET" request to "/dummies?page[page]=0"
Then the response should be in JSON
And the response status code should be 400
And the JSON node title should be equal to 'An error occurred'
And the JSON node description should be equal to 'Page request attribute must be a numeric value equal or greater than 1'
5 changes: 5 additions & 0 deletions src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* Applies pagination on the Doctrine query for resource collection when enabled.
Expand Down Expand Up @@ -262,6 +263,10 @@ private function useOutputWalkers(QueryBuilder $queryBuilder): bool
private function getPaginationParameter(Request $request, string $parameterName, $default = null)
{
if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) {
if (isset($paginationAttribute['page']) && ((!is_numeric($paginationAttribute['page']) || (1 > (int) $paginationAttribute['page'])))) {
throw new BadRequestHttpException('Page request attribute must be a numeric value equal or greater than 1');
}

return array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default;
}

Expand Down