-
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
Fixes nested has_many links in JSONAPI #730
Conversation
When linked resource had has_many links, all of them would use the association from the first resource, causing all of the items to build `links` with the same associations. This fixes it by iterating over the serializers, not just the attributes array.
body: "Body", | ||
id: "3", | ||
links: { | ||
comments: [], |
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.
The bug was here. Before, comments
was ["1", "2"]
, not []
.
Need a pair of eyes here @guilleiguaran @ggordon |
@@ -58,25 +58,30 @@ def add_link(resource, name, serializer) | |||
end | |||
end | |||
|
|||
def add_linked(resource_name, serializer, parent = nil) | |||
def add_linked(resource_name, serializers, parent = nil) | |||
serializers = Array(serializers) unless serializers.respond_to?(:each) |
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.
Is 'unless ...' needed here? If serializers is already an Array, Array(serializers) would just retunr serilizers.
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.
So, It can be Serializer
or ArraySerializer
. If it's Serializer
, I convert to [Serializer]
so I can treat them all equally, without the need for duplicating the logic, eg.
if serializer.respond_to?(:each)
serializer.each
do something
end
else
do something
end
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 see, Array(array_serializer) would be wrong.
LGTM 👍 |
Fixes nested has_many links in JSONAPI
When linked resource had has_many links, all of them would use the
association from the first resource, causing all of the items to build
links
with the same associations.This fixes it by iterating over the serializers, not just the
attributes array.