Repository navigation
Closed
Description
This breaks certain APIs that expect a parameter passed in with nil to remain nil. To reproduce the problem:
diff --git a/spec/grape/validations/validators/coerce_spec.rb b/spec/grape/validations/validators/coerce_spec.rb
index dbf57c9..cb2cca5 100644
--- a/spec/grape/validations/validators/coerce_spec.rb
+++ b/spec/grape/validations/validators/coerce_spec.rb
@@ -647,6 +647,19 @@ describe Grape::Validations::CoerceValidator do
expect(last_response.body).to eq('String')
end
+ it 'respects nil values' do
+ subject.params do
+ optional :a, types: [File, String]
+ end
+ subject.get '/' do
+ params[:a].class.to_s
+ end
+
+ get '/', a: nil
+ expect(last_response.status).to eq(200)
+ expect(last_response.body).to eq('NilClass')
+ end
+
it 'fails when no coercion is possible' do
subject.params do
requires :a, types: [Boolean, Integer]This is the test failure:
2) Grape::Validations::CoerceValidator coerce multiple types respects nil values
Failure/Error: expect(last_response.body).to eq('NilClass')
expected: "NilClass"
got: "Array"
(compared using ==)
# ./spec/grape/validations/validators/coerce_spec.rb:660:in `block (4 levels) in <top (required)>'
It looks like this happens because of this code block:
grape/lib/grape/validations/validators/coerce.rb
Lines 68 to 71 in 8deebb5
| # define default values for structures, the dry-types lib which is used | |
| # for coercion doesn't accept nil as a value, so it would fail | |
| if val.nil? | |
| return [] if type == Array || type.is_a?(Array) |
I'm not quite sure I understand why the type.is_a?(Array) check needs to be there. This allows tests to pass:
$ git diff
diff --git a/lib/grape/validations/validators/coerce.rb b/lib/grape/validations/validators/coerce.rb
index 561ec1b..b2b4cbf 100644
--- a/lib/grape/validations/validators/coerce.rb
+++ b/lib/grape/validations/validators/coerce.rb
@@ -68,9 +68,10 @@ module Grape
# define default values for structures, the dry-types lib which is used
# for coercion doesn't accept nil as a value, so it would fail
if val.nil?
- return [] if type == Array || type.is_a?(Array)
+ return [] if type == Array
return Set.new if type == Set
return {} if type == Hash
+ return val
end
converter.call(val)Also, if we need to accept nil as a coerced value, there is https://github.com/dry-rb/dry-types/blob/bd75d556e70c2d3bef1f96140a8f33fb44c31f9d/docsite/source/optional-values.html.md.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array
Avoid coercing parameter with multiple types to an empty Array