Skip to content

Commit

Permalink
Fix alias on dependent param bug (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren authored and dblock committed Oct 31, 2018
1 parent 3bb835a commit b8b85af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b8b85af

Please sign in to comment.