File tree 3 files changed +41
-10
lines changed
lib/grape/validations/validators
spec/grape/validations/validators 3 files changed +41
-10
lines changed Original file line number Diff line number Diff line change 10
10
#### Fixes
11
11
12
12
* Your contribution here.
13
+ * [ #2019 ] ( https://github.com/ruby-grape/grape/pull/2018 ) : Avoid coercing parameter with multiple types to an empty Array - [ @stanhu ] ( https://github.com/stanhu ) .
13
14
14
15
### 1.3.1 (2020/03/11)
15
16
Original file line number Diff line number Diff line change @@ -65,21 +65,21 @@ def valid_type?(val)
65
65
end
66
66
67
67
def coerce_value ( val )
68
- # define default values for structures, the dry-types lib which is used
69
- # for coercion doesn't accept nil as a value, so it would fail
70
- if val . nil?
71
- return [ ] if type == Array || type . is_a? ( Array )
72
- return Set . new if type == Set
73
- return { } if type == Hash
74
- end
75
-
76
- converter . call ( val )
77
-
68
+ val . nil? ? coerce_nil ( val ) : converter . call ( val )
78
69
# Some custom types might fail, so it should be treated as an invalid value
79
70
rescue StandardError
80
71
Types ::InvalidValue . new
81
72
end
82
73
74
+ def coerce_nil ( val )
75
+ # define default values for structures, the dry-types lib which is used
76
+ # for coercion doesn't accept nil as a value, so it would fail
77
+ return [ ] if type == Array
78
+ return Set . new if type == Set
79
+ return { } if type == Hash
80
+ val
81
+ end
82
+
83
83
# Type to which the parameter will be coerced.
84
84
#
85
85
# @return [Class]
Original file line number Diff line number Diff line change @@ -180,6 +180,23 @@ def self.parsed?(value)
180
180
expect ( last_response . body ) . to eq ( integer_class_name )
181
181
end
182
182
183
+ it 'String' do
184
+ subject . params do
185
+ requires :string , coerce : String
186
+ end
187
+ subject . get '/string' do
188
+ params [ :string ] . class
189
+ end
190
+
191
+ get '/string' , string : 45
192
+ expect ( last_response . status ) . to eq ( 200 )
193
+ expect ( last_response . body ) . to eq ( 'String' )
194
+
195
+ get '/string' , string : nil
196
+ expect ( last_response . status ) . to eq ( 200 )
197
+ expect ( last_response . body ) . to eq ( 'NilClass' )
198
+ end
199
+
183
200
it 'is a custom type' do
184
201
subject . params do
185
202
requires :uri , coerce : SecureURIOnly
@@ -647,6 +664,19 @@ def self.parsed?(value)
647
664
expect ( last_response . body ) . to eq ( 'String' )
648
665
end
649
666
667
+ it 'respects nil values' do
668
+ subject . params do
669
+ optional :a , types : [ File , String ]
670
+ end
671
+ subject . get '/' do
672
+ params [ :a ] . class . to_s
673
+ end
674
+
675
+ get '/' , a : nil
676
+ expect ( last_response . status ) . to eq ( 200 )
677
+ expect ( last_response . body ) . to eq ( 'NilClass' )
678
+ end
679
+
650
680
it 'fails when no coercion is possible' do
651
681
subject . params do
652
682
requires :a , types : [ Boolean , Integer ]
You can’t perform that action at this time.
0 commit comments