Skip to content

Commit

Permalink
BigDecimal coercion (#1971)
Browse files Browse the repository at this point in the history
Mapping for BigDecimal to DryTypes::Coercible::Decimal for coercion
  • Loading branch information
FlickStuart authored and dnesteryuk committed Jan 14, 2020
1 parent 5fc668b commit 341160c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### Fixes

* Your contribution here.
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
* [#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).

### 1.3.0 (2020/01/11)
Expand Down
6 changes: 4 additions & 2 deletions lib/grape/validations/types/primitive_coercer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class PrimitiveCoercer < DryTypeCoercer
Grape::API::Boolean => DryTypes::Params::Bool,

# unfortunatelly, a +Params+ scope doesn't contain String
String => DryTypes::Coercible::String
String => DryTypes::Coercible::String,
BigDecimal => DryTypes::Coercible::Decimal
}.freeze

STRICT_MAPPING = {
Grape::API::Boolean => DryTypes::Strict::Bool
Grape::API::Boolean => DryTypes::Strict::Bool,
BigDecimal => DryTypes::Strict::Decimal
}.freeze

def initialize(type, strict = false)
Expand Down
13 changes: 13 additions & 0 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ def self.parsed?(value)
end

context 'coerces' do
it 'BigDecimal' do
subject.params do
requires :bigdecimal, coerce: BigDecimal
end
subject.get '/bigdecimal' do
params[:bigdecimal].class
end

get '/bigdecimal', bigdecimal: '45'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('BigDecimal')
end

it 'Integer' do
subject.params do
requires :int, coerce: Integer
Expand Down

0 comments on commit 341160c

Please sign in to comment.