Skip to content

Commit 44e0498

Browse files
committed
Add is_a?(Array) to coerce_nil because of Array with a type.
Add CHANGELOG.md
1 parent e0dfb9c commit 44e0498

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Fixes
1010

1111
* Your contribution here.
12+
* [#2040](https://github.com/ruby-grape/grape/pull/2040): Fix a regression with Array of type nil - [@ericproulx](https://github.com/ericproulx).
1213

1314
### 1.3.2 (2020/04/12)
1415

lib/grape/validations/validators/coerce.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def coerce_value(val)
7676
def coerce_nil(val)
7777
# define default values for structures, the dry-types lib which is used
7878
# for coercion doesn't accept nil as a value, so it would fail
79-
return [] if type == Array
79+
return [] if type == Array || type.is_a?(Array)
8080
return Set.new if type == Set
8181
return {} if type == Hash
8282
val

spec/grape/validations/validators/coerce_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,22 @@ def self.parsed?(value)
311311
expect(last_response.status).to eq(400)
312312
expect(last_response.body).to eq('uri is invalid')
313313
end
314+
315+
context 'Array of types nil' do
316+
it 'Empty array' do
317+
subject.params do
318+
requires :arry, type: Array[Integer]
319+
end
320+
subject.get '/array' do
321+
params[:arry]
322+
end
323+
324+
get '/array', arry: nil
325+
expect(last_response.status).to eq(200)
326+
expect(last_response.body).to eq('[]')
327+
end
328+
end
329+
314330
end
315331

316332
context 'Set' do

0 commit comments

Comments
 (0)