Skip to content

Commit 11d181c

Browse files
committed
Merge pull request #1096 from towanda/fix-boolean-type
Fix boolean type
2 parents 41c7bd6 + 72f884a commit 11d181c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ Next Release
1616
* [#1038](https://github.com/ruby-grape/grape/pull/1038): Avoid dup-ing the String class when used in inherited params - [@rnubel](https://github.com/rnubel).
1717
* [#1042](https://github.com/ruby-grape/grape/issues/1042): Fix coercion of complex arrays - [@dim](https://github.com/dim).
1818
* [#1045](https://github.com/ruby-grape/grape/pull/1045): Do not convert `Rack::Response` to `Rack::Response` in middleware - [@dmitry](https://github.com/dmitry).
19-
* [#1048](https://github.com/ruby-grape/grape/pull/1048): Only dup `InheritableValues`, remove support for `deep_dup` - [@toddmazierski](https://github.com/toddmazierski/)
19+
* [#1048](https://github.com/ruby-grape/grape/pull/1048): Only dup `InheritableValues`, remove support for `deep_dup` - [@toddmazierski](https://github.com/toddmazierski/).
2020
* [#1052](https://github.com/ruby-grape/grape/pull/1052): Reset `description[:params]` when resetting validations - [@marshall-lee](https://github.com/marshall-lee).
21-
* [#1088](https://github.com/ruby-grape/grape/pull/1088): Support ActiveSupport 3.x by explicitly requiring Hash#except - [@wagenet](https://github.com/wagenet)
21+
* [#1088](https://github.com/ruby-grape/grape/pull/1088): Support ActiveSupport 3.x by explicitly requiring Hash#except - [@wagenet](https://github.com/wagenet).
22+
* [#1096](https://github.com/ruby-grape/grape/pull/1096): Fix coercion on booleans - [@towanda](https://github.com/towanda).
2223

2324
0.12.0 (6/18/2015)
2425
==================

lib/grape/validations/params_scope.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ def validate_value_coercion(coerce_type, values)
254254
return if values.is_a?(Proc)
255255
coerce_type = coerce_type.first if coerce_type.is_a?(Array)
256256
value_types = values.is_a?(Range) ? [values.begin, values.end] : values
257+
if coerce_type == Virtus::Attribute::Boolean
258+
value_types = value_types.map { |type| Virtus::Attribute.build(type) }
259+
end
257260
if value_types.any? { |v| !v.is_a?(coerce_type) }
258261
fail Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values)
259262
end

spec/grape/validations/validators/values_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class API < Grape::API
5656
{ type: params[:type] }
5757
end
5858

59+
params do
60+
optional :type, type: Boolean, desc: 'A boolean', values: [true]
61+
end
62+
get '/values/optional_boolean' do
63+
{ type: params[:type] }
64+
end
65+
5966
params do
6067
requires :type, type: Integer, desc: 'An integer', values: [10, 11], default: 10
6168
end
@@ -174,6 +181,11 @@ def app
174181
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
175182
end
176183

184+
it 'allows values to be true or false when setting the type to boolean' do
185+
get('/values/optional_boolean', type: true)
186+
expect(last_response.status).to eq 200
187+
expect(last_response.body).to eq({ type: true }.to_json)
188+
end
177189
it 'allows values to be a kind of the coerced type not just an instance of it' do
178190
get('/values/coercion', type: 10)
179191
expect(last_response.status).to eq 200

0 commit comments

Comments
 (0)