-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Avoid coercing parameter with multiple types to an empty Array #2019
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
Avoid coercing parameter with multiple types to an empty Array #2019
Conversation
af12335
to
10b15fe
Compare
return Set.new if type == Set | ||
return {} if type == Hash | ||
return val if type.is_a?(Array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this type.is_a?(Array)
check. I assume that passing a nil
value to a parameter with multiple types might lead to ambiguity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it was necessary
it 'handles default value to array' do
subject.params do
optional :a, type: Array[Integer], default: []
end
subject.put('/test') { declared(params).to_json }
put '/test', { a: nil }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('{"a":[]}')
end
That test fails because type != array
but it's type.is_a?(Array)
I'll make a PR to fix it
10b15fe
to
d38b594
Compare
Looks like a bug, thanks for the fix! Move the changelog entry and I'll merge. |
d38b594
to
43fd676
Compare
109d1a5
to
31d9ff0
Compare
See rubocop offense, I'll merge on green. |
return [] if type == Array | ||
return Set.new if type == Set | ||
return {} if type == Hash | ||
return val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just val
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a good place for a case
case type
when Array
when ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't actually work in this case because we need to distinguish between type.is_a?(Array)
and type == Array
(https://stackoverflow.com/questions/3908380/ruby-class-types-and-case-statements).
31d9ff0
to
d0e8009
Compare
If an optional parameter has multiple types (e.g. [File, String]) and a nil parameter is passed, Grape would previously coerce the value to an empty Array. This can break certain APIs that treat nil values differently from an empty Array. To fix this, we refactor the nil handling into a `coerce_nil` method and remove the special case check for handling an Array of types. Closes ruby-grape#2018
d0e8009
to
633d7cf
Compare
If an optional parameter has multiple types (e.g.
[File, String]
) and anil
parameter is passed, Grape would previously coerce the value to anempty
Array
. This can break certain APIs that treat nil valuesdifferently from an empty Array.
To fix this, we refactor the nil handling into a
coerce_nil
method andremove the special case check for handling an Array of types.
Closes #2018