Skip to content

Commit c8fd21b

Browse files
authored
Fix Decimal type category (#2025)
1 parent cd03bcf commit c8fd21b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#### Fixes
1111

1212
* Your contribution here.
13+
* [#2025](https://github.com/ruby-grape/grape/pull/2025): Fix Decimal type category - [@kdoya](https://github.com/kdoya).
1314
* [#2019](https://github.com/ruby-grape/grape/pull/2019): Avoid coercing parameter with multiple types to an empty Array - [@stanhu](https://github.com/stanhu).
1415

1516
### 1.3.1 (2020/03/11)

lib/grape/validations/types/primitive_coercer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ module Types
1111
class PrimitiveCoercer < DryTypeCoercer
1212
MAPPING = {
1313
Grape::API::Boolean => DryTypes::Params::Bool,
14+
BigDecimal => DryTypes::Params::Decimal,
1415

1516
# unfortunately, a +Params+ scope doesn't contain String
16-
String => DryTypes::Coercible::String,
17-
BigDecimal => DryTypes::Coercible::Decimal
17+
String => DryTypes::Coercible::String
1818
}.freeze
1919

2020
STRICT_MAPPING = {

spec/grape/validations/validators/coerce_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ def self.parsed?(value)
154154
end
155155

156156
context 'coerces' do
157+
context 'json' do
158+
let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json' } }
159+
160+
it 'BigDecimal' do
161+
subject.params do
162+
requires :bigdecimal, type: BigDecimal
163+
end
164+
subject.post '/bigdecimal' do
165+
params[:bigdecimal]
166+
end
167+
168+
post '/bigdecimal', { bigdecimal: 45.1 }.to_json, headers
169+
expect(last_response.status).to eq(201)
170+
expect(last_response.body).to eq('45.1')
171+
end
172+
173+
it 'Boolean' do
174+
subject.params do
175+
requires :boolean, type: Boolean
176+
end
177+
subject.post '/boolean' do
178+
params[:boolean]
179+
end
180+
181+
post '/boolean', { boolean: 'true' }.to_json, headers
182+
expect(last_response.status).to eq(201)
183+
expect(last_response.body).to eq('true')
184+
end
185+
end
186+
157187
it 'BigDecimal' do
158188
subject.params do
159189
requires :bigdecimal, coerce: BigDecimal

0 commit comments

Comments
 (0)