Skip to content

Ensure floats are correctly coerced to bigdecimal #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#### Fixes

* Your contribution here.
* [#2033](https://github.com/ruby-grape/grape/pull/2033): Ensure `Float` params are correctly coerced to `BigDecimal` - [@tlconnor](https://github.com/tlconnor).
* [#2031](https://github.com/ruby-grape/grape/pull/2031): Fix a regression with an array of a custom type - [@dnesteryuk](https://github.com/dnesteryuk).
* [#2026](https://github.com/ruby-grape/grape/pull/2026): Fix a regression in `coerce_with` when coercion returns `nil` - [@misdoro](https://github.com/misdoro).
* [#2025](https://github.com/ruby-grape/grape/pull/2025): Fix Decimal type category - [@kdoya](https://github.com/kdoya).
Expand Down
4 changes: 3 additions & 1 deletion lib/grape/validations/validators/coerce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def validate_param!(attr_name, params)
# h[:list] = list
# h
# => #<Hashie::Mash list=[1, 2, 3, 4]>
params[attr_name] = new_value unless params[attr_name] == new_value
return if params[attr_name].class == new_value.class && params[attr_name] == new_value

params[attr_name] = new_value
end

private
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ def self.parsed?(value)
requires :bigdecimal, type: BigDecimal
end
subject.post '/bigdecimal' do
params[:bigdecimal]
"#{params[:bigdecimal].class} #{params[:bigdecimal].to_f}"
end

post '/bigdecimal', { bigdecimal: 45.1 }.to_json, headers
expect(last_response.status).to eq(201)
expect(last_response.body).to eq('45.1')
expect(last_response.body).to eq('BigDecimal 45.1')
end

it 'Boolean' do
Expand Down