diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a9004ccc..db84664f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,9 +16,10 @@ Next Release * [#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). * [#1042](https://github.com/ruby-grape/grape/issues/1042): Fix coercion of complex arrays - [@dim](https://github.com/dim). * [#1045](https://github.com/ruby-grape/grape/pull/1045): Do not convert `Rack::Response` to `Rack::Response` in middleware - [@dmitry](https://github.com/dmitry). -* [#1048](https://github.com/ruby-grape/grape/pull/1048): Only dup `InheritableValues`, remove support for `deep_dup` - [@toddmazierski](https://github.com/toddmazierski/) +* [#1048](https://github.com/ruby-grape/grape/pull/1048): Only dup `InheritableValues`, remove support for `deep_dup` - [@toddmazierski](https://github.com/toddmazierski/). * [#1052](https://github.com/ruby-grape/grape/pull/1052): Reset `description[:params]` when resetting validations - [@marshall-lee](https://github.com/marshall-lee). -* [#1088](https://github.com/ruby-grape/grape/pull/1088): Support ActiveSupport 3.x by explicitly requiring Hash#except - [@wagenet](https://github.com/wagenet) +* [#1088](https://github.com/ruby-grape/grape/pull/1088): Support ActiveSupport 3.x by explicitly requiring Hash#except - [@wagenet](https://github.com/wagenet). +* [#1096](https://github.com/ruby-grape/grape/pull/1096): Fix coercion on booleans - [@towanda](https://github.com/towanda). 0.12.0 (6/18/2015) ================== diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 349c709c3..2a63b0c26 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -254,6 +254,9 @@ def validate_value_coercion(coerce_type, values) return if values.is_a?(Proc) coerce_type = coerce_type.first if coerce_type.is_a?(Array) value_types = values.is_a?(Range) ? [values.begin, values.end] : values + if coerce_type == Virtus::Attribute::Boolean + value_types = value_types.map { |type| Virtus::Attribute.build(type) } + end if value_types.any? { |v| !v.is_a?(coerce_type) } fail Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values) end diff --git a/spec/grape/validations/validators/values_spec.rb b/spec/grape/validations/validators/values_spec.rb index a10fc328a..b0ef7f31c 100644 --- a/spec/grape/validations/validators/values_spec.rb +++ b/spec/grape/validations/validators/values_spec.rb @@ -56,6 +56,13 @@ class API < Grape::API { type: params[:type] } end + params do + optional :type, type: Boolean, desc: 'A boolean', values: [true] + end + get '/values/optional_boolean' do + { type: params[:type] } + end + params do requires :type, type: Integer, desc: 'An integer', values: [10, 11], default: 10 end @@ -174,6 +181,11 @@ def app end.to raise_error Grape::Exceptions::IncompatibleOptionValues end + it 'allows values to be true or false when setting the type to boolean' do + get('/values/optional_boolean', type: true) + expect(last_response.status).to eq 200 + expect(last_response.body).to eq({ type: true }.to_json) + end it 'allows values to be a kind of the coerced type not just an instance of it' do get('/values/coercion', type: 10) expect(last_response.status).to eq 200