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

Support nested dependent parameters #1811

Merged
merged 2 commits into from
Nov 1, 2018
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 @@ -15,6 +15,7 @@
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
* [#1810](https://github.com/ruby-grape/grape/pull/1810): Fix support in `given` for aliased params - [@darren987469](https://github.com/darren987469).
* [#1811](https://github.com/ruby-grape/grape/pull/1811): Support nested dependent parameters - [@darren987469](https://github.com/darren987469), [@andreacfm](https://github.com/andreacfm).

### 1.1.0 (8/4/2018)

Expand Down
13 changes: 9 additions & 4 deletions lib/grape/dsl/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,15 @@ def given(*attrs, &block)
# block yet.
# @return [Boolean] whether the parameter has been defined
def declared_param?(param)
# @declared_params also includes hashes of options and such, but those
# won't be flattened out.
@declared_params.flatten.any? do |declared_param|
first_hash_key_or_param(declared_param) == param
if lateral?
# Elements of @declared_params of lateral scope are pushed in @parent. So check them in @parent.
@parent.declared_param?(param)
else
# @declared_params also includes hashes of options and such, but those
# won't be flattened out.
@declared_params.flatten.any? do |declared_param|
first_hash_key_or_param(declared_param) == param
end
end
end

Expand Down
43 changes: 43 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,49 @@ def initialize(value)
end.to_not raise_error
end

it 'does not raise an error if when using nested given' do
expect do
subject.params do
optional :a, type: Hash do
requires :b
end
given :a do
requires :c
given :c do
requires :d
end
end
end
end.to_not raise_error
end

it 'allows nested dependent parameters' do
subject.params do
optional :a
given a: ->(val) { val == 'a' } do
optional :b
given b: ->(val) { val == 'b' } do
optional :c
given c: ->(val) { val == 'c' } do
requires :d
end
end
end
end
subject.get('/') { declared(params).to_json }

get '/'
expect(last_response.status).to eq 200

get '/', a: 'a', b: 'b', c: 'c'
expect(last_response.status).to eq 400
expect(last_response.body).to eq 'd is missing'

get '/', a: 'a', b: 'b', c: 'c', d: 'd'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ a: 'a', b: 'b', c: 'c', d: 'd' }.to_json)
end

it 'allows aliasing of dependent parameters' do
subject.params do
optional :a
Expand Down