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

Fixed edge case regarding Hash with inner param #1156

Merged
merged 1 commit into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#### Fixes

* [#1156](https://github.com/ruby-grape/grape/pull/1156): Fixed `no implicit conversion of Symbol into Integer` with nested `values` validation - [@quickpay](https://github.com/quickpay).
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).
* [#1114](https://github.com/ruby-grape/grape/pull/1114): Fix regression which broke identical endpoints with different versions - [@suan](https://github.com/suan).
Expand Down
1 change: 1 addition & 0 deletions lib/grape/validations/validators/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(attrs, options, required, scope)
end

def validate_param!(attr_name, params)
return unless params.is_a?(Hash)
Copy link
Member

Choose a reason for hiding this comment

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

We are skipping the error here, shouldn't it be a fail?

Copy link
Member

Choose a reason for hiding this comment

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

Actually I take it back. The parent is the problem, we don't get deep enough to care about the values validator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just skips the validation of the inner param if the outer param does not have a valid value.

The "real" error happens in the coerce validator that correctly invalidates a Hash param with string value.

This is the same thing happening in the allow_blank validator here: https://github.com/ruby-grape/grape/blob/master/lib/grape/validations/validators/allow_blank.rb#L5

Copy link
Member

Choose a reason for hiding this comment

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

Yes I should first have coffee and then review code :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:) Always coffee first.

return unless params[attr_name] || required_for_root_scope?

values = @values.is_a?(Proc) ? @values.call : @values
Expand Down
29 changes: 29 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,35 @@ def define_requires_none
end
end

context 'hash with a required param with validation' do
before do
subject.params do
requires :items, type: Hash do
requires :key, type: String, values: %w(a b)
end
end
subject.get '/required' do
'required works'
end
end

it 'errors when param is not a Hash' do
get '/required', items: 'not a hash'
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('items is invalid, items[key] is missing, items[key] is invalid')

get '/required', items: [{ key: 'hash in array' }]
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('items is invalid, items[key] does not have a valid value')
end

it 'works when all params match' do
get '/required', items: { key: 'a' }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('required works')
end
end

context 'group' do
before do
subject.params do
Expand Down