Skip to content

Commit 6df96a0

Browse files
committed
Coerce empty string to nil for all Primitive types except String
This matches the behavior in v1.2.5 with the exception of Symbol. In v1.2.5 an empty string was coerced to an empty symbol (:''), now it is coerced to nil.
1 parent 693180f commit 6df96a0

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
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+
* [#2067](https://github.com/ruby-grape/grape/pull/2067): Coerce empty string to nil for all primitive types except String
1213
* [#2064](https://github.com/ruby-grape/grape/pull/2064): Fix Ruby 2.7 deprecation warning in `Grape::Middleware::Base#initialize` - [@skarger](https://github.com/skarger).
1314

1415
### 1.3.3 (2020/05/23)

lib/grape/validations/types/primitive_coercer.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def initialize(type, strict = false)
3737
def call(val)
3838
return InvalidValue.new if reject?(val)
3939
return nil if val.nil? || treat_as_nil?(val)
40-
return '' if val == ''
4140

4241
super
4342
end
@@ -60,7 +59,7 @@ def reject?(val)
6059
# absence of a value and coerces it into nil. See a discussion there
6160
# https://github.com/ruby-grape/grape/pull/2045
6261
def treat_as_nil?(val)
63-
val == '' && type == Grape::API::Boolean
62+
val == '' && type != String
6463
end
6564
end
6665
end

spec/grape/validations/types/primitive_coercer_spec.rb

+56
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
it 'coerces to BigDecimal' do
1515
expect(subject.call(5)).to eq(BigDecimal(5))
1616
end
17+
18+
it 'coerces an empty string to nil' do
19+
expect(subject.call('')).to be_nil
20+
end
1721
end
1822

1923
context 'Boolean' do
@@ -40,12 +44,64 @@
4044
end
4145
end
4246

47+
context 'DateTime' do
48+
let(:type) { DateTime }
49+
50+
it 'coerces an empty string to nil' do
51+
expect(subject.call('')).to be_nil
52+
end
53+
end
54+
55+
context 'Float' do
56+
let(:type) { Float }
57+
58+
it 'coerces an empty string to nil' do
59+
expect(subject.call('')).to be_nil
60+
end
61+
end
62+
63+
context 'Integer' do
64+
let(:type) { Integer }
65+
66+
it 'coerces an empty string to nil' do
67+
expect(subject.call('')).to be_nil
68+
end
69+
end
70+
71+
context 'Numeric' do
72+
let(:type) { Numeric }
73+
74+
it 'coerces an empty string to nil' do
75+
expect(subject.call('')).to be_nil
76+
end
77+
end
78+
79+
context 'Time' do
80+
let(:type) { Time }
81+
82+
it 'coerces an empty string to nil' do
83+
expect(subject.call('')).to be_nil
84+
end
85+
end
86+
4387
context 'String' do
4488
let(:type) { String }
4589

4690
it 'coerces to String' do
4791
expect(subject.call(10)).to eq('10')
4892
end
93+
94+
it 'does not coerce an empty string to nil' do
95+
expect(subject.call('')).to eq('')
96+
end
97+
end
98+
99+
context 'Symbol' do
100+
let(:type) { Symbol }
101+
102+
it 'coerces an empty string to nil' do
103+
expect(subject.call('')).to be_nil
104+
end
49105
end
50106

51107
context 'the strict mode' do

0 commit comments

Comments
 (0)