-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix for Deserialization erroring when a relationship is null in the json api document. #1651
Fix for Deserialization erroring when a relationship is null in the json api document. #1651
Conversation
There's related issues and PRs for this too, gotta 🏃 |
well, I need this fixed, so I'm working on fixing it. :-) |
lots of rubocop errors :-\ |
@@ -188,7 +188,7 @@ def parse_relationship(assoc_name, assoc_data, options) | |||
end | |||
|
|||
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym) | |||
hash["#{prefix_key}_type".to_sym] = assoc_data[:type] if polymorphic | |||
hash["#{prefix_key}_type".to_sym] = assoc_data.try(:[], :type) if polymorphic |
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.
in Ember, if an association isn't set: upon saving, the data for that association is just straight up nil
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.
no more try
, try!
if you must
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's per JSON API spec
Resource linkage MUST be represented as one of the following:
- null for empty to-one relationships.
- an empty array ([]) for empty to-many relationships.
- a single resource identifier object for non-empty to-one relationships.
- an array of resource identifier objects for non-empty to-many relationships.
so is assoc_data possibly nil?
- hash["#{prefix_key}_type".to_sym] = assoc_data[:type] if polymorphic
+ hash["#{prefix_key}_type".to_sym] = assoc_data[:type] if assoc_data && polymorphic
looks like on rails master, assoc_data is "", whereas on non-master assoc_data is nil |
4cf774e
to
e7c1f2e
Compare
squashed -- all tests should pass now. I believe this could go out with rc5, @bf4 |
@@ -188,7 +188,9 @@ def parse_relationship(assoc_name, assoc_data, options) | |||
end | |||
|
|||
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym) | |||
hash["#{prefix_key}_type".to_sym] = assoc_data[:type] if polymorphic | |||
if polymorphic | |||
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data[:type] : nil |
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 don't understand why you're not checking polymorphic && assoc_data. Is the idea that we want to set it to nil? so, is assoc data an empty string then? assoc_data.empty?
would work
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.
assoc_data is nil on non-master rails, and empty string on master rails. but yeah, the idea is that if the assoc_data isn't there, it would still be nice to get the _id and _type attributes in the resulting hash, so at least you know your front end is aware of the relationship (but hasn't set it)
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 did try polymorphic && assoc_data earlier, it ended up giving me just the id field and not the type field.
changelog and ok to merge |
cool |
failing test use try for when the assoc_data is possibly nil rubocop test/action_controller/json_api/deserialization_test.rb -a attempt to work on rails-master account for rails/master having instead of nil for assoc_data added changelog
58f54dc
to
5be33af
Compare
changelog added |
Purpose
I noticed when I was trying to parse the request for building a join model, I had this error:
Changes
added test
Caveats
Related GitHub issues
Additional helpful information
How I'm parsing the params
This happened with the latest (as of this time) version of AMS.