From 4283b4217991370fa81ce8bee4a00df750cd5b95 Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 31 Oct 2018 18:46:47 +0800 Subject: [PATCH] Fix alias on dependent param bug (#1810) --- CHANGELOG.md | 1 + README.md | 12 +++++++++ lib/grape/validations/params_scope.rb | 1 + spec/grape/validations/params_scope_spec.rb | 28 +++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb891a7531..49b1cc9d42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469). * [#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). ### 1.1.0 (8/4/2018) diff --git a/README.md b/README.md index 42555d2ee3..e3fe7d36fd 100644 --- a/README.md +++ b/README.md @@ -1175,6 +1175,18 @@ params do end ``` +You can set alias for parameter: + +```ruby +params do + optional :category, as: :type + given type: ->(val) { val == 'foo' } do + requires :description + end +end +``` + +Note: param in `given` should be the aliased one. In the example, it should be `type`, not `category`. ### Group Options diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index cad7acb484..05c0c8c0a7 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -121,6 +121,7 @@ def push_declared_params(attrs, **opts) if opts && opts[:as] @api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || []) @api.route_setting(:aliased_params) << { attrs.first => opts[:as] } + attrs = [opts[:as]] end @declared_params.concat attrs diff --git a/spec/grape/validations/params_scope_spec.rb b/spec/grape/validations/params_scope_spec.rb index fff6f5df3e..9975c66c9e 100644 --- a/spec/grape/validations/params_scope_spec.rb +++ b/spec/grape/validations/params_scope_spec.rb @@ -497,6 +497,34 @@ def initialize(value) expect(body.keys).to_not include('b') end + it 'allows aliasing of dependent on parameter' do + subject.params do + optional :a, as: :b + given b: ->(val) { val == 'x' } do + requires :c + end + end + subject.get('/') { declared(params) } + + get '/', a: 'x' + expect(last_response.status).to eq 400 + expect(last_response.body).to eq 'c is missing' + + get '/', a: 'y' + expect(last_response.status).to eq 200 + end + + it 'raises an error if the dependent parameter is not the aliased one' do + expect do + subject.params do + optional :a, as: :b + given :a do + requires :c + end + end + end.to raise_error(Grape::Exceptions::UnknownParameter) + 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]