-
Notifications
You must be signed in to change notification settings - Fork 801
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
Coerce pagination string params #110
Conversation
👍 |
@@ -69,7 +69,7 @@ def limit(value) | |||
@results = nil | |||
@records = nil | |||
@response = nil | |||
@per_page = value | |||
@per_page = value.to_i |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may introduce some confusion as any value other than a string representation of a number is likely going to return 0 by calling the #to_i:
"35".to_i # good
> 35
"ab".to_i # not good
> 0
This will cause the next few lines to succeed, with a size of 0 and a per_page of 0, and likely add confusion. I think it should fail loudly. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know of a way to get ruby to throw up when converting a String
to a Fixnum
if the conversion doesn't make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever we decide here needs to also be applied to the #page
method above in the same file (it already uses #to_i
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if instead of just coercing we validated it as well?
@per_page.to_i
must be >= 1
If it is < 1, then it has no affect on the query and neither does the page argument. Thus returning all results. The page number is only valid when there is a proper per_page limit.
When I put in a page number that doesn't make sense as a possible value say -1
or boom
kaminari just gives me page 1. So following that I would really want it to be ignored and fall back to a default value. Default value for @page
is easy. Picking a default value for @per_page
would be arbitrary.
Also, I wouldn't want to throw an exception because someone decided to put ?per_page=boom
in their address bar; I get enough email as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fail loudly:
@per_page = Integer(value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AaronRustad That's entirely too obvious. 😏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like @morganick's idea of testing for >=1
since that also deals with negative integers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AaronRustad @morganick I've added code/tests for invalid string params.
dcda5a1
to
50920d6
Compare
Coming back to ES after some time on other projects. This PR still needs a merge. I've rebased the topic branch and pushed it. |
I've signed the CLA. |
50920d6
to
68c8dea
Compare
@ryansch Finally got to merge this, sorry for the ridiculous delay! |
We hit this in our app when we pass in params from a rails controller. This PR causes the
limit
andoffset
methods to work like thepage
(or::Kaminari.config.page_method_name
) method already does.