-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix exception being thrown when dependent parameter is a Hash #1740
Conversation
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.
Thanks, great stuff!
I made some minor-ish comments to address, please.
CHANGELOG.md
Outdated
@@ -8,6 +8,8 @@ | |||
|
|||
* Your contribution here. | |||
|
|||
* [#1740](/~https://github.com/ruby-grape/grape/pull/1740): Fix dependent parameter validation using `given` when parameter is a `Hash` - [@jvortmann](/~https://github.com/jvortmann). |
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.
Nitpick, put "Your contribution here" below the line? Just more consistent with how we've been doing it.
@@ -466,6 +466,20 @@ def initialize(value) | |||
end.to raise_error(Grape::Exceptions::UnknownParameter) | |||
end | |||
|
|||
it 'DOES NOT raise an error if the dependent parameter is a Hash' do |
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.
Lets not shout. So "does not raise error if ..." Thanks :)
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.
No problem.
The idea was not to shout, but to make it more visible when running using "documentation" format.
it 'DOES NOT raise an error if the dependent parameter is a Hash' do | ||
expect do | ||
subject.params do | ||
optional :d, type: Integer |
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 am nitpicking, but is this optional :d
needed for the spec?
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 did to populate declared_params with something more than a Hash.
Just removed as I believe there are other specs covering thath.
lib/grape/dsl/parameters.rb
Outdated
private | ||
|
||
def first_hash_key_or_param(parameter) | ||
parameter.is_a?(Hash) ? parameter.keys[0] : parameter |
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.
Maybe .keys.first
is more idiomatic?
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.
Yes, it might.
The case is that I've just move the line that were in the given
method as is
.
Should I change the line then?
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.
It's nbd
When using `given` and the paramter type is a Hash, a Grape::Exceptions::UnknownParameter is thrown. This is a wrong behavior due to the fact that the method `declared_param?(param)` do not deal properly with params that are hashes (even after flattening the declared params). This commit fixes this by checking if the param is a Hash and thus returning its first key. This approach was already used in other part of the code, so it was extracted and reused on `declared_params?(param)`.
@dblock just did the changes :) |
Merged, thank you. |
@dblock nice :) What are the plans to cut a release? Is there a particular time-box for that? |
No plans, maintainers cut releases fairly frequently so stay tuned. |
I have the same problem, so a gem release with this fix would be pleased |
When using
given
and the paramter type is a Hash,a Grape::Exceptions::UnknownParameter is thrown. This is a wrong behavior
due to the fact that the method
declared_param?(param)
do not dealproperly with params that are hashes (even after flattening the
declared params).
This commit fixes this by checking if the param is a Hash and thus
returning its first key. This approach was already used in other part of
the code, so it was extracted and reused on
declared_params?(param)
.