diff --git a/CHANGELOG.md b/CHANGELOG.md index 1581dfca54..dc39f22549 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index e34c2b6238..cad7acb484 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -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) || []) diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index 111fadf2e8..fff6f5df3e 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -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]