diff --git a/lib/jsonapi-serializers/serializer.rb b/lib/jsonapi-serializers/serializer.rb index 3e55158..67c3894 100644 --- a/lib/jsonapi-serializers/serializer.rb +++ b/lib/jsonapi-serializers/serializer.rb @@ -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.to_s.split('.').first } passthrough_options[:include_linkages] = direct_children_includes end diff --git a/spec/serializer_spec.rb b/spec/serializer_spec.rb index 10a782e..7cf9646 100644 --- a/spec/serializer_spec.rb +++ b/spec/serializer_spec.rb @@ -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 @@ -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, @@ -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. @@ -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. @@ -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