-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Returns array for hash parameter when not present #1596
Comments
You should pass raw |
Thanks for the quick response @namusyaka, using By the way, I was using params do
group :user, type: Hash do
# ...
optional :location_attributes, type: Hash, default: {} do
# ...
end
end
end
put do
User.find(params.fetch(:id)).tap do |u|
u.update! declared(params).fetch(:user)
end
end However, It's not clear why the change from using |
I encountered this issue today also on grape v0.17.0. If I hit the following api only supplying params do
requires :body, type: String
optional :metadata, type: Hash do
optional :media_type, type: Integer
end
end
put do
changed_attrs = declared(params).compact
changed_attrs[:metadata] # => []
end I would expect |
Just encountered another strange thing testing it, if I pass metadata as explicitly put "/api/notes/#{id}", body: 'foo', metadata: nil Then the value in For referenceGiven the following api params do
optional :metadata, type: Hash do
optional :media_type, type: Integer
end
end
put do
present :params, params
present :declared, declared(params)
end When
|
Using grape 0.19.1, also hapening in 0.19.0.
Whats happening
Given I declared a parameter key with type
Hash
and the parameter was not included when calling the controller, I't returns an empty array for that key.Expected behavior
It should return an empty hash, or
nil
, or perhaps not include the key in the parsed output.Extended Explanation
Given this parameters:
... when I hit the controller with this params:
... and I extract the declared params like this:
the result is this:
Note that
location_attributes
gets parsed into an empty array. I do not know exactly what is the expected behavior, but an empty array is not what I've expected. I'd either havenil
or an empty hash. It breaks with activerecord'saccepts_nested_attributes_for :location
with:I am missing something obvious here?
The text was updated successfully, but these errors were encountered: