Skip to content

Commit 341160c

Browse files
FlickStuartdnesteryuk
authored andcommitted
BigDecimal coercion (#1971)
Mapping for BigDecimal to DryTypes::Coercible::Decimal for coercion
1 parent 5fc668b commit 341160c

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
1010
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
1111

1212
### 1.3.0 (2020/01/11)

lib/grape/validations/types/primitive_coercer.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ class PrimitiveCoercer < DryTypeCoercer
1212
Grape::API::Boolean => DryTypes::Params::Bool,
1313

1414
# unfortunatelly, a +Params+ scope doesn't contain String
15-
String => DryTypes::Coercible::String
15+
String => DryTypes::Coercible::String,
16+
BigDecimal => DryTypes::Coercible::Decimal
1617
}.freeze
1718

1819
STRICT_MAPPING = {
19-
Grape::API::Boolean => DryTypes::Strict::Bool
20+
Grape::API::Boolean => DryTypes::Strict::Bool,
21+
BigDecimal => DryTypes::Strict::Decimal
2022
}.freeze
2123

2224
def initialize(type, strict = false)

spec/grape/validations/validators/coerce_spec.rb

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

156156
context 'coerces' do
157+
it 'BigDecimal' do
158+
subject.params do
159+
requires :bigdecimal, coerce: BigDecimal
160+
end
161+
subject.get '/bigdecimal' do
162+
params[:bigdecimal].class
163+
end
164+
165+
get '/bigdecimal', bigdecimal: '45'
166+
expect(last_response.status).to eq(200)
167+
expect(last_response.body).to eq('BigDecimal')
168+
end
169+
157170
it 'Integer' do
158171
subject.params do
159172
requires :int, coerce: Integer

0 commit comments

Comments
 (0)