Skip to content

Commit b8b85af

Browse files
Darrendblock
Darren
authored andcommitted
Fix alias on dependent param bug (#1810)
1 parent 3bb835a commit b8b85af

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
1515
* [#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).
1616
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
17+
* [#1810](https://github.com/ruby-grape/grape/pull/1810): Fix support in `given` for aliased params - [@darren987469](https://github.com/darren987469).
1718

1819
### 1.1.0 (8/4/2018)
1920

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,18 @@ params do
11751175
end
11761176
```
11771177

1178+
You can set alias for parameter:
1179+
1180+
```ruby
1181+
params do
1182+
optional :category, as: :type
1183+
given type: ->(val) { val == 'foo' } do
1184+
requires :description
1185+
end
1186+
end
1187+
```
1188+
1189+
Note: param in `given` should be the aliased one. In the example, it should be `type`, not `category`.
11781190

11791191
### Group Options
11801192

lib/grape/validations/params_scope.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def push_declared_params(attrs, **opts)
121121
if opts && opts[:as]
122122
@api.route_setting(:aliased_params, @api.route_setting(:aliased_params) || [])
123123
@api.route_setting(:aliased_params) << { attrs.first => opts[:as] }
124+
attrs = [opts[:as]]
124125
end
125126

126127
@declared_params.concat attrs

spec/grape/validations/params_scope_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,34 @@ def initialize(value)
497497
expect(body.keys).to_not include('b')
498498
end
499499

500+
it 'allows aliasing of dependent on parameter' do
501+
subject.params do
502+
optional :a, as: :b
503+
given b: ->(val) { val == 'x' } do
504+
requires :c
505+
end
506+
end
507+
subject.get('/') { declared(params) }
508+
509+
get '/', a: 'x'
510+
expect(last_response.status).to eq 400
511+
expect(last_response.body).to eq 'c is missing'
512+
513+
get '/', a: 'y'
514+
expect(last_response.status).to eq 200
515+
end
516+
517+
it 'raises an error if the dependent parameter is not the aliased one' do
518+
expect do
519+
subject.params do
520+
optional :a, as: :b
521+
given :a do
522+
requires :c
523+
end
524+
end
525+
end.to raise_error(Grape::Exceptions::UnknownParameter)
526+
end
527+
500528
it 'does not validate nested requires when given is false' do
501529
subject.params do
502530
requires :a, type: String, allow_blank: false, values: %w[x y z]

0 commit comments

Comments
 (0)