Skip to content

Commit

Permalink
Fix param aliases within 'given' blocks (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
jereynolds authored and dblock committed Jul 23, 2018
1 parent 2dcef9a commit a152d98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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

0 comments on commit a152d98

Please sign in to comment.