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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/jsonapi-serializers/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!

passthrough_options[:include_linkages] = include_linkages
end

# Spec: Primary data MUST be either:
Expand Down
19 changes: 14 additions & 5 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ def read_attribute_for_validation(attr)
long_comments.each { |c| c.post = post }

expected_data = {
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
'data' => serialize_primary(post, {
serializer: MyApp::PostSerializer,
include_linkages: ['long-comments']
}),
'included' => [
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
# http://jsonapi.org/format/#document-structure-compound-documents
Expand Down Expand Up @@ -745,7 +748,10 @@ def read_attribute_for_validation(attr)
long_comments.each { |c| c.post = post }

expected_data = {
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
'data' => serialize_primary(post, {
serializer: MyApp::PostSerializer,
include_linkages: ['long-comments']
}),
'included' => [
serialize_primary(first_comment, {
serializer: MyApp::LongCommentSerializer,
Expand Down Expand Up @@ -788,7 +794,7 @@ def read_attribute_for_validation(attr)
serialize_primary(comment_user, {serializer: MyAppOtherNamespace::UserSerializer}),
],
}
includes = ['long-comments', 'long-comments.user']
includes = ['long-comments.user']
actual_data = JSONAPI::Serializer.serialize(post, include: includes)

# Multiple expectations for better diff output for debugging.
Expand Down Expand Up @@ -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.

includes = 'long-comments,long-comments.post.author'
actual_data = JSONAPI::Serializer.serialize(post, include: includes)

# Multiple expectations for better diff output for debugging.
Expand Down Expand Up @@ -1197,7 +1203,10 @@ def read_attribute_for_validation(attr)
long_comments.each { |c| c.post = post }

expected_data = {
'data' => serialize_primary(post, {serializer: Api::V1::MyApp::PostSerializer}),
'data' => serialize_primary(post, {
serializer: Api::V1::MyApp::PostSerializer,
include_linkages: ['long-comments']
}),
'included' => [
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
# http://jsonapi.org/format/#document-structure-compound-documents
Expand Down