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 param aliases within 'given' blocks #1771

Merged
merged 3 commits into from
Jul 23, 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 @@ -11,6 +11,7 @@
* [#1759](https://github.com/ruby-grape/grape/pull/1759): Update appraisal for rails_edge - [@zvkemp](https://github.com/zvkemp).
* [#1758](https://github.com/ruby-grape/grape/pull/1758): Fix expanding load_path in gemspec - [@2maz](https://github.com/2maz).
* [#1765](https://github.com/ruby-grape/grape/pull/1765): Use 415 when request body is of an unsupported media type - [@jdmurphy](https://github.com/jdmurphy).
* [#1771](https://github.com/ruby-grape/grape/pull/1771): Fix param aliases with 'given' blocks - [@jereynolds](https://github.com/jereynolds).
* Your contribution here.

### 1.0.3 (4/23/2018)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def required?
# @param attrs [Array] (see Grape::DSL::Parameters#requires)
def push_declared_params(attrs, **opts)
if lateral?
@parent.push_declared_params(attrs)
@parent.push_declared_params(attrs, opts)
else
if opts && opts[:as]
@api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || [])
Expand Down
18 changes: 18 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,24 @@ def initialize(value)
end.to_not raise_error
end

it 'allows aliasing of dependent parameters' do
subject.params do
optional :a
given :a do
requires :b, as: :c
end
end

subject.get('/multiple') { declared(params).to_json }

get '/multiple', a: 'a', b: 'b'

body = JSON.parse(last_response.body)

expect(body.keys).to include('c')
expect(body.keys).to_not include('b')
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]
Expand Down