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

Add documentation in README.md
Add specs for future proof
  • Loading branch information
ericproulx committed Apr 17, 2020
1 parent e0dfb9c commit aba1f3c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- [Include Parent Namespaces](#include-parent-namespaces)
- [Include Missing](#include-missing)
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
- [Structures Implicit Default Value](#structures-implicit-default-value)
- [Supported Parameter Types](#supported-parameter-types)
- [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
- [Custom Types and Coercions](#custom-types-and-coercions)
Expand Down Expand Up @@ -1070,6 +1071,26 @@ params do
end
```

### Structures Implicit Default Value

There is an implicit default value when coercing for the following types with a nil value

* Array = []
* Array[ types ] = []
* Set = Set
* Hash = {}

```ruby
params do
requires :arry, type: Array
end
get '/array' do
params[:arry]
end

get '/array', { arry: nil }
#=> []
```
### Supported Parameter Types

The following are all valid types, supported out of the box by Grape:
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ 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
end

Expand Down
37 changes: 24 additions & 13 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,30 @@ def self.parsed?(value)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq(integer_class_name)
end

context 'structures default value' do
[
[Array, 'Array'],
[Set, 'Set'],
[Hash, 'ActiveSupport::HashWithIndifferentAccess'],
[Array[Integer], 'Array']
].each do |type, expected_class|
context 'structures default value' do
it 'respects the default value' do
subject.params do
requires :struct, type: type
end
subject.get '/default_value' do
params[:struct].class
end

get '/default_value', struct: nil
expect(last_response.status).to eq(200)
expect(last_response.body).to eq(expected_class)
end
end
end
end
end

context 'using coerce_with' do
Expand Down Expand Up @@ -752,19 +776,6 @@ def self.parsed?(value)
expect(last_response.body).to eq('String')
end

it 'respects nil values' do
subject.params do
optional :a, types: [File, String]
end
subject.get '/' do
params[:a].class.to_s
end

get '/', a: nil
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('NilClass')
end

it 'fails when no coercion is possible' do
subject.params do
requires :a, types: [Boolean, Integer]
Expand Down

0 comments on commit aba1f3c

Please sign in to comment.