Skip to content

Commit 56a2656

Browse files
committed
Throw 400 status erro when a non numeric value is encountered on page request attribute in pagination
1 parent 59e3840 commit 56a2656

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

features/jsonapi/pagination.feature

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ Feature: JSON API pagination handling
3232
And the JSON node "meta.totalItems" should be equal to the number 10
3333
And the JSON node "meta.itemsPerPage" should be equal to the number 15
3434
And the JSON node "meta.currentPage" should be equal to the number 1
35+
36+
Scenario: Get a paginated collection with an non numeric page request attribute
37+
When I send a "GET" request to "/dummies?page[page]=dummie_non_numeric"
38+
Then the response should be in JSON
39+
And the response status code should be 400
40+
And the JSON node title should be equal to 'An error occurred'
41+
And the JSON node description should be equal to 'Page request attribute must be a numeric value'

src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
2626
use Symfony\Component\HttpFoundation\Request;
2727
use Symfony\Component\HttpFoundation\RequestStack;
28+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2829

2930
/**
3031
* Applies pagination on the Doctrine query for resource collection when enabled.
@@ -262,6 +263,10 @@ private function useOutputWalkers(QueryBuilder $queryBuilder): bool
262263
private function getPaginationParameter(Request $request, string $parameterName, $default = null)
263264
{
264265
if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) {
266+
if (isset($paginationAttribute['page']) && !is_numeric($paginationAttribute['page'])) {
267+
throw new BadRequestHttpException('Page request attribute must be a numeric value');
268+
}
269+
265270
return array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default;
266271
}
267272

0 commit comments

Comments
 (0)