Skip to content

Commit a152d98

Browse files
jereynoldsdblock
authored andcommitted
Fix param aliases within 'given' blocks (#1771)
1 parent 2dcef9a commit a152d98

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#1759](https://github.com/ruby-grape/grape/pull/1759): Update appraisal for rails_edge - [@zvkemp](https://github.com/zvkemp).
1212
* [#1758](https://github.com/ruby-grape/grape/pull/1758): Fix expanding load_path in gemspec - [@2maz](https://github.com/2maz).
1313
* [#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).
14+
* [#1771](https://github.com/ruby-grape/grape/pull/1771): Fix param aliases with 'given' blocks - [@jereynolds](https://github.com/jereynolds).
1415
* Your contribution here.
1516

1617
### 1.0.3 (4/23/2018)

lib/grape/validations/params_scope.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def required?
116116
# @param attrs [Array] (see Grape::DSL::Parameters#requires)
117117
def push_declared_params(attrs, **opts)
118118
if lateral?
119-
@parent.push_declared_params(attrs)
119+
@parent.push_declared_params(attrs, opts)
120120
else
121121
if opts && opts[:as]
122122
@api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || [])

spec/grape/validations/params_scope_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,24 @@ def initialize(value)
479479
end.to_not raise_error
480480
end
481481

482+
it 'allows aliasing of dependent parameters' do
483+
subject.params do
484+
optional :a
485+
given :a do
486+
requires :b, as: :c
487+
end
488+
end
489+
490+
subject.get('/multiple') { declared(params).to_json }
491+
492+
get '/multiple', a: 'a', b: 'b'
493+
494+
body = JSON.parse(last_response.body)
495+
496+
expect(body.keys).to include('c')
497+
expect(body.keys).to_not include('b')
498+
end
499+
482500
it 'does not validate nested requires when given is false' do
483501
subject.params do
484502
requires :a, type: String, allow_blank: false, values: %w[x y z]

0 commit comments

Comments
 (0)