Skip to content

Commit aba1f3c

Browse files
committed
Add is_a?(Array) to coerce_nil because of Array with a type.
Add CHANGELOG.md Add documentation in README.md Add specs for future proof
1 parent e0dfb9c commit aba1f3c

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [Include Parent Namespaces](#include-parent-namespaces)
3939
- [Include Missing](#include-missing)
4040
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
41+
- [Structures Implicit Default Value](#structures-implicit-default-value)
4142
- [Supported Parameter Types](#supported-parameter-types)
4243
- [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
4344
- [Custom Types and Coercions](#custom-types-and-coercions)
@@ -1070,6 +1071,26 @@ params do
10701071
end
10711072
```
10721073

1074+
### Structures Implicit Default Value
1075+
1076+
There is an implicit default value when coercing for the following types with a nil value
1077+
1078+
* Array = []
1079+
* Array[ types ] = []
1080+
* Set = Set
1081+
* Hash = {}
1082+
1083+
```ruby
1084+
params do
1085+
requires :arry, type: Array
1086+
end
1087+
get '/array' do
1088+
params[:arry]
1089+
end
1090+
1091+
get '/array', { arry: nil }
1092+
#=> []
1093+
```
10731094
### Supported Parameter Types
10741095

10751096
The following are all valid types, supported out of the box by Grape:

lib/grape/validations/validators/coerce.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ 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
82+
8283
val
8384
end
8485

spec/grape/validations/validators/coerce_spec.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,30 @@ def self.parsed?(value)
424424
expect(last_response.status).to eq(200)
425425
expect(last_response.body).to eq(integer_class_name)
426426
end
427+
428+
context 'structures default value' do
429+
[
430+
[Array, 'Array'],
431+
[Set, 'Set'],
432+
[Hash, 'ActiveSupport::HashWithIndifferentAccess'],
433+
[Array[Integer], 'Array']
434+
].each do |type, expected_class|
435+
context 'structures default value' do
436+
it 'respects the default value' do
437+
subject.params do
438+
requires :struct, type: type
439+
end
440+
subject.get '/default_value' do
441+
params[:struct].class
442+
end
443+
444+
get '/default_value', struct: nil
445+
expect(last_response.status).to eq(200)
446+
expect(last_response.body).to eq(expected_class)
447+
end
448+
end
449+
end
450+
end
427451
end
428452

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

755-
it 'respects nil values' do
756-
subject.params do
757-
optional :a, types: [File, String]
758-
end
759-
subject.get '/' do
760-
params[:a].class.to_s
761-
end
762-
763-
get '/', a: nil
764-
expect(last_response.status).to eq(200)
765-
expect(last_response.body).to eq('NilClass')
766-
end
767-
768779
it 'fails when no coercion is possible' do
769780
subject.params do
770781
requires :a, types: [Boolean, Integer]

0 commit comments

Comments
 (0)