Skip to content

[Bugfix] Handle deeply-nested dependent params #1661

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

Merged
merged 3 commits into from
Jul 14, 2017
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 @@ -8,6 +8,7 @@
#### Fixes

* [#1652](https://github.com/ruby-grape/grape/pull/1652): Fix missing backtrace that was not being bubbled up to the `error_formatter` - [@dcsg](https://github.com/dcsg).
* [#1661](https://github.com/ruby-grape/grape/pull/1661): Handle deeply-nested dependencies correctly - [@rnubel](https://github.com/rnubel), [@jnardone](https://github.com/jnardone).

### 1.0.0 (7/3/2017)

Expand Down
6 changes: 5 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def should_validate?(parameters)
parent.should_validate?(parameters)
end

def meets_dependency?(params)
def meets_dependency?(params, request_params)
if @parent.present? && [email protected]_dependency?(@parent.params(request_params), request_params)
return false
end

return true unless @dependent_on

params = params.with_indifferent_access
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def validate!(params)
array_errors = []
attributes.each do |resource_params, attr_name|
next unless @required || (resource_params.respond_to?(:key?) && resource_params.key?(attr_name))
next unless @scope.meets_dependency?(resource_params)
next unless @scope.meets_dependency?(resource_params, params)

begin
validate_param!(attr_name, resource_params)
Expand Down
40 changes: 40 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,46 @@ def initialize(value)
end.to raise_error(Grape::Exceptions::UnknownParameter)
end

it 'does not validate nested requires when given is false' do
subject.params do
requires :a, type: String, allow_blank: false, values: %w(x y z)
given a: ->(val) { val == 'x' } do
requires :inner1, type: Hash, allow_blank: false do
requires :foo, type: Integer, allow_blank: false
end
end
given a: ->(val) { val == 'y' } do
requires :inner2, type: Hash, allow_blank: false do
requires :bar, type: Integer, allow_blank: false
requires :baz, type: Array, allow_blank: false do
requires :baz_category, type: String, allow_blank: false
end
end
end
given a: ->(val) { val == 'z' } do
requires :inner3, type: Array, allow_blank: false do
requires :bar, type: Integer, allow_blank: false
requires :baz, type: Array, allow_blank: false do
requires :baz_category, type: String, allow_blank: false
end
end
end
end
subject.get('/varying') { declared(params).to_json }

get '/varying', a: 'x', inner1: { foo: 1 }
expect(last_response.status).to eq(200)

get '/varying', a: 'y', inner2: { bar: 2, baz: [{ baz_category: 'barstools' }] }
expect(last_response.status).to eq(200)

get '/varying', a: 'y', inner2: { bar: 2, baz: [{ unrelated: 'yep' }] }
expect(last_response.status).to eq(400)

get '/varying', a: 'z', inner3: [{ bar: 3, baz: [{ baz_category: 'barstools' }] }]
expect(last_response.status).to eq(200)
end

it 'includes the parameter within #declared(params)' do
get '/test', a: true, b: true

Expand Down