Skip to content

Commit

Permalink
Add is_a?(Array) to coerce_nil because of Array with a type.
Browse files Browse the repository at this point in the history
Add CHANGELOG.md
  • Loading branch information
ericproulx committed Apr 16, 2020
1 parent e0dfb9c commit 44e0498
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#### Fixes

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

### 1.3.2 (2020/04/12)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def coerce_value(val)
def coerce_nil(val)
# define default values for structures, the dry-types lib which is used
# for coercion doesn't accept nil as a value, so it would fail
return [] if type == Array
return [] if type == Array || type.is_a?(Array)
return Set.new if type == Set
return {} if type == Hash
val
Expand Down
16 changes: 16 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ def self.parsed?(value)
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('uri is invalid')
end

context 'Array of types nil' do
it 'Empty array' do
subject.params do
requires :arry, type: Array[Integer]
end
subject.get '/array' do
params[:arry]
end

get '/array', arry: nil
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('[]')
end
end

end

context 'Set' do
Expand Down

0 comments on commit 44e0498

Please sign in to comment.