Skip to content

Commit

Permalink
Fix compound documents includes
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed May 4, 2018
1 parent b3beb56 commit 83aca8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/jsonapi-serializers/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ 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?('.') }
direct_children_includes = includes.map { |key| key.include?('.') ? key.split('.').first : key }
passthrough_options[:include_linkages] = direct_children_includes
end

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'
includes = '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

0 comments on commit 83aca8d

Please sign in to comment.