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

[RFR] Use 1 as page value if the submited one is invalid #1826

Closed

Conversation

ottaviano
Copy link

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Hi, it's a little fix to avoid this error LIMIT argument offset=-24 is not valid if invalid page value is submitted.

@@ -103,7 +103,9 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
throw new InvalidArgumentException('Item per page parameter should not be less than 0');
}

$page = $this->getPaginationParameter($request, $this->pageParameterName, 1);
if (0 >= $page = $this->getPaginationParameter($request, $this->pageParameterName, 1)) {
$page = 1;
Copy link
Member

Choose a reason for hiding this comment

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

what about throwing an error instead?

Copy link
Author

Choose a reason for hiding this comment

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

@soyuka, it can be useful if the front call the api with empty page param for first page: /resources?page=

Copy link
Contributor

Choose a reason for hiding this comment

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

it can be useful if the front call the api with empty page param for first page: /resources?page=

I'm actually surprised that we're not casting to integer somewhere...

$page = $this->getPaginationParameter($request, $this->pageParameterName, 1);
if (0 >= $page = $this->getPaginationParameter($request, $this->pageParameterName, 1)) {
$page = 1;
}

if (0 === $itemsPerPage && 1 < $page) {
Copy link
Member

Choose a reason for hiding this comment

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

This test does exactly the same thing isn't it?

Copy link
Author

Choose a reason for hiding this comment

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

@dunglas not exactly,
Need to check the negative or zero value for $page which the $firstResult can be negative.

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

@@ -103,7 +103,9 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
throw new InvalidArgumentException('Item per page parameter should not be less than 0');
}

$page = $this->getPaginationParameter($request, $this->pageParameterName, 1);
Copy link
Member

Choose a reason for hiding this comment

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

I would just cast the result of this expression to (int), or throw after calling ctype_digit?

Copy link
Author

Choose a reason for hiding this comment

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

@dunglas, cast don't fix the negative page value ?page=-10

Copy link
Contributor

@teohhanhui teohhanhui May 2, 2018

Choose a reason for hiding this comment

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

We can cast, and also check for negative values. If the value is 0 we could default to 1. But if it's negative, maybe throw an exception?

@ottaviano ottaviano changed the title Use 1 as page value if the submited one is invalid [RFR] Use 1 as page value if the submited one is invalid Apr 9, 2018
@teohhanhui
Copy link
Contributor

I think we can close this since #1871 is already merged.

@teohhanhui teohhanhui closed this May 2, 2018
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

4 participants