Skip to content

Commit 25c831e

Browse files
committed
add page number type validation
1 parent 3fdd546 commit 25c831e

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

features/hydra/collection.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,4 @@ Feature: Collections support
434434

435435
When I send a "GET" request to "/dummies?itemsPerPage=0&page=2"
436436
Then the response status code should be 400
437-
And the JSON node "hydra:description" should be equal to "Page should not be greater than 1 if itemsPegPage is equal to 0"
437+
And the JSON node "hydra:description" should be equal to "Page should not be greater than 1 if itemsPerPage is equal to 0"

features/jsonapi/pagination.feature

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ 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 an error when provided page number is not valid
37+
When I send a "GET" request to "/dummies?page[page]=0"
38+
Then the response status code should be 400

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
103103
throw new InvalidArgumentException('Item per page parameter should not be less than 0');
104104
}
105105

106-
$page = $this->getPaginationParameter($request, $this->pageParameterName, 1);
106+
$page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1);
107+
108+
if (1 > $page) {
109+
throw new InvalidArgumentException('Page should not be less than 1');
110+
}
107111

108112
if (0 === $itemsPerPage && 1 < $page) {
109-
throw new InvalidArgumentException('Page should not be greater than 1 if itemsPegPage is equal to 0');
113+
throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0');
110114
}
111115

112116
$firstResult = ($page - 1) * $itemsPerPage;

tests/Bridge/Doctrine/Orm/Extension/PaginationExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testApplyToCollectionWithItemPerPageZero()
101101

102102
/**
103103
* @expectedException \ApiPlatform\Core\Exception\InvalidArgumentException
104-
* @expectedExceptionMessage Page should not be greater than 1 if itemsPegPage is equal to 0
104+
* @expectedExceptionMessage Page should not be greater than 1 if itemsPerPage is equal to 0
105105
*/
106106
public function testApplyToCollectionWithItemPerPageZeroAndPage2()
107107
{

0 commit comments

Comments
 (0)