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

Fix exception being thrown when dependent parameter is a Hash #1740

Merged
merged 1 commit into from
Feb 6, 2018

Conversation

jvortmann
Copy link
Contributor

@jvortmann jvortmann commented Feb 6, 2018

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).

Copy link
Member

@dblock dblock left a 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).
Copy link
Member

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
Copy link
Member

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 :)

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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.

private

def first_hash_key_or_param(parameter)
parameter.is_a?(Hash) ? parameter.keys[0] : parameter
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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)`.
@jvortmann
Copy link
Contributor Author

@dblock just did the changes :)

@dblock dblock merged commit 1dd02dd into ruby-grape:master Feb 6, 2018
@dblock
Copy link
Member

dblock commented Feb 6, 2018

Merged, thank you.

@jvortmann
Copy link
Contributor Author

@dblock nice :)

What are the plans to cut a release? Is there a particular time-box for that?

@dblock
Copy link
Member

dblock commented Feb 7, 2018

No plans, maintainers cut releases fairly frequently so stay tuned.

@douglaslise
Copy link

I have the same problem, so a gem release with this fix would be pleased

@dblock
Copy link
Member

dblock commented Mar 9, 2018

#1748

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.

3 participants