Skip to content

Commit d3765bf

Browse files
committed
Fix usage of exactly_one_of in shared params
1 parent a68d36d commit d3765bf

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#1755](https://github.com/ruby-grape/grape/pull/1755): Fix shared params with exactly_one_of - [@milgner](https://github.com/milgner).
910
* [#1740](https://github.com/ruby-grape/grape/pull/1740): Fix dependent parameter validation using `given` when parameter is a `Hash` - [@jvortmann](https://github.com/jvortmann).
1011
* [#1737](https://github.com/ruby-grape/grape/pull/1737): Fix translating error when passing symbols as params in custom validations - [@mlzhuyi](https://github.com/mlzhuyi).
1112
* [#1749](https://github.com/ruby-grape/grape/pull/1749): Allow rescue from non-`StandardError` exceptions - [@dm1try](https://github.com/dm1try).

lib/grape/validations/params_scope.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def initialize(opts, &block)
4040
# validated
4141
def should_validate?(parameters)
4242
return false if @optional && (params(parameters).blank? || all_element_blank?(parameters))
43-
43+
return false unless meets_dependency?(params(parameters), parameters)
4444
return true if parent.nil?
4545
parent.should_validate?(parameters)
4646
end
@@ -51,7 +51,7 @@ def meets_dependency?(params, request_params)
5151
end
5252

5353
return true unless @dependent_on
54-
54+
return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array)
5555
params = params.with_indifferent_access
5656

5757
@dependent_on.each do |dependency|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe Grape::API::Helpers do
4+
subject do
5+
shared_params = Module.new do
6+
extend Grape::API::Helpers
7+
8+
params :drink do
9+
optional :beer
10+
optional :wine
11+
exactly_one_of :beer, :wine
12+
end
13+
end
14+
15+
Class.new(Grape::API) do
16+
helpers shared_params
17+
format :json
18+
19+
params do
20+
requires :orderType, type: String, values: %w[food drink]
21+
given orderType: ->(val) { val == 'food' } do
22+
optional :pasta
23+
optional :pizza
24+
exactly_one_of :pasta, :pizza
25+
end
26+
given orderType: ->(val) { val == 'drink' } do
27+
use :drink
28+
end
29+
end
30+
get do
31+
declared(params, include_missing: true)
32+
end
33+
end
34+
end
35+
36+
def app
37+
subject
38+
end
39+
40+
it 'defines parameters' do
41+
get '/', orderType: 'food', pizza: 'mista'
42+
expect(last_response.status).to eq 200
43+
expect(last_response.body).to eq({ orderType: 'food',
44+
pasta: nil, pizza: 'mista',
45+
beer: nil, wine: nil }.to_json)
46+
end
47+
end

0 commit comments

Comments
 (0)