Skip to content

Commit b3616b2

Browse files
author
Tim Connor
committed
ensure floats are correctly coerced to bigdecimal
1 parent c5cd332 commit b3616b2

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
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+
* [#2033](https://github.com/ruby-grape/grape/pull/2033): Ensure floats are correctly coerced to bigdecimal - [@tlconnor](https://github.com/tlconnor).
1314
* [#2031](https://github.com/ruby-grape/grape/pull/2031): Fix a regression with an array of a custom type - [@dnesteryuk](https://github.com/dnesteryuk).
1415
* [#2026](https://github.com/ruby-grape/grape/pull/2026): Fix a regression in `coerce_with` when coercion returns `nil` - [@misdoro](https://github.com/misdoro).
1516
* [#2025](https://github.com/ruby-grape/grape/pull/2025): Fix Decimal type category - [@kdoya](https://github.com/kdoya).

lib/grape/validations/validators/coerce.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def validate_param!(attr_name, params)
4747
# h[:list] = list
4848
# h
4949
# => #<Hashie::Mash list=[1, 2, 3, 4]>
50-
params[attr_name] = new_value unless params[attr_name] == new_value
50+
return if params[attr_name].class == new_value.class && params[attr_name] == new_value
51+
52+
params[attr_name] = new_value
5153
end
5254

5355
private

spec/grape/validations/validators/coerce_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ def self.parsed?(value)
162162
requires :bigdecimal, type: BigDecimal
163163
end
164164
subject.post '/bigdecimal' do
165-
params[:bigdecimal]
165+
"#{params[:bigdecimal].class} #{params[:bigdecimal]}"
166166
end
167167

168168
post '/bigdecimal', { bigdecimal: 45.1 }.to_json, headers
169169
expect(last_response.status).to eq(201)
170-
expect(last_response.body).to eq('45.1')
170+
expect(last_response.body).to eq('BigDecimal 0.451e2')
171171
end
172172

173173
it 'Boolean' do

0 commit comments

Comments
 (0)