Skip to content
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 compound relationships includes #116

Merged
merged 2 commits into from
Jun 4, 2018

Conversation

Exelord
Copy link
Contributor

@Exelord Exelord commented May 4, 2018

Hey,
I noticed that including compound relationships of the resource doesn't add correct linkage data.

Resources model

-- author
---- has_many posts
------ has_many comments

Current behavior

calling a url GET /author/4?include=posts.comments doesn't return linkage for posts relationship of author resource

Expected standard

According to JSONAPI spec: http://jsonapi.org/format/#fetching-includes

Because compound documents require full linkage (except when relationship linkage is excluded by sparse fieldsets), intermediate resources in a multi-part path must be returned along with the leaf nodes. For example, a response to a request for comments.author should include comments as well as the author of each of those comments.

Note: A server may choose to expose a deeply nested relationship such as comments.author as a direct relationship with an alias such as comment-authors. This would allow a client to request /articles/1?include=comment-authors instead of /articles/1?include=comments.author. By abstracting the nested relationship with an alias, the server can still provide full linkage in compound documents without including potentially unwanted intermediate resources.

Proposed behavior

calling a url GET /author/4?include=posts.comments does return linkage for posts relationship of author resource

@Exelord Exelord force-pushed the fix-compound-includes branch from 83aca8d to e566217 Compare May 4, 2018 22:25
@Exelord Exelord force-pushed the fix-compound-includes branch from e566217 to 3f5c1ea Compare May 4, 2018 22:25
@Exelord Exelord changed the title Fix compound documents includes Fix compound relationships includes May 4, 2018
@Azdaroth
Copy link
Contributor

Azdaroth commented May 6, 2018

Hi @fotinakis, any chance of having it merged soon? This would be a super helpful addition for us. Thanks.

Copy link
Owner

@fotinakis fotinakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Exelord thanks for this, sorry for the delay reviewing.

Can you check out the one test comment here and see what the correct behavior for that should be? After that LGTM

@@ -826,7 +832,7 @@ def read_attribute_for_validation(attr)
],
}
# Also test that it handles string include arguments.
includes = 'long-comments, long-comments.post.author'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behavior of the test, it's specifically testing the behavior of "overlapping include paths" but this no longer does that.

@@ -304,8 +304,8 @@ def self.serialize(objects, options = {})

# Automatically include linkage data for any relation that is also included.
if includes
direct_children_includes = includes.reject { |key| key.include?('.') }
passthrough_options[:include_linkages] = direct_children_includes
include_linkages = includes.map { |key| key.to_s.split('.').first }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@fotinakis
Copy link
Owner

@Exelord friendly ping here

@Exelord
Copy link
Contributor Author

Exelord commented May 31, 2018

@fotinakis Done.

@fotinakis fotinakis merged commit 2ad376d into fotinakis:master Jun 4, 2018
@Exelord Exelord deleted the fix-compound-includes branch June 5, 2018 12:32
@fotinakis
Copy link
Owner

Published in jsonapi-serializers v1.0.1. Thanks for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants