Skip to content

Commit 2ad376d

Browse files
Exelordfotinakis
authored andcommitted
Fix compound relationships includes (#116)
* Fix compound documents includes * Apply feedback
1 parent b3beb56 commit 2ad376d

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

lib/jsonapi-serializers/serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ def self.serialize(objects, options = {})
304304

305305
# Automatically include linkage data for any relation that is also included.
306306
if includes
307-
direct_children_includes = includes.reject { |key| key.include?('.') }
308-
passthrough_options[:include_linkages] = direct_children_includes
307+
include_linkages = includes.map { |key| key.to_s.split('.').first }
308+
passthrough_options[:include_linkages] = include_linkages
309309
end
310310

311311
# Spec: Primary data MUST be either:

spec/serializer_spec.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,10 @@ def read_attribute_for_validation(attr)
708708
long_comments.each { |c| c.post = post }
709709

710710
expected_data = {
711-
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
711+
'data' => serialize_primary(post, {
712+
serializer: MyApp::PostSerializer,
713+
include_linkages: ['long-comments']
714+
}),
712715
'included' => [
713716
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
714717
# http://jsonapi.org/format/#document-structure-compound-documents
@@ -745,7 +748,10 @@ def read_attribute_for_validation(attr)
745748
long_comments.each { |c| c.post = post }
746749

747750
expected_data = {
748-
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
751+
'data' => serialize_primary(post, {
752+
serializer: MyApp::PostSerializer,
753+
include_linkages: ['long-comments']
754+
}),
749755
'included' => [
750756
serialize_primary(first_comment, {
751757
serializer: MyApp::LongCommentSerializer,
@@ -788,7 +794,7 @@ def read_attribute_for_validation(attr)
788794
serialize_primary(comment_user, {serializer: MyAppOtherNamespace::UserSerializer}),
789795
],
790796
}
791-
includes = ['long-comments', 'long-comments.user']
797+
includes = ['long-comments.user']
792798
actual_data = JSONAPI::Serializer.serialize(post, include: includes)
793799

794800
# Multiple expectations for better diff output for debugging.
@@ -826,7 +832,7 @@ def read_attribute_for_validation(attr)
826832
],
827833
}
828834
# Also test that it handles string include arguments.
829-
includes = 'long-comments, long-comments.post.author'
835+
includes = 'long-comments,long-comments.post.author'
830836
actual_data = JSONAPI::Serializer.serialize(post, include: includes)
831837

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

11991205
expected_data = {
1200-
'data' => serialize_primary(post, {serializer: Api::V1::MyApp::PostSerializer}),
1206+
'data' => serialize_primary(post, {
1207+
serializer: Api::V1::MyApp::PostSerializer,
1208+
include_linkages: ['long-comments']
1209+
}),
12011210
'included' => [
12021211
# Intermediates are included: long-comments, long-comments.post, and long-comments.post.author
12031212
# http://jsonapi.org/format/#document-structure-compound-documents

0 commit comments

Comments
 (0)