Skip to content

Commit 33dcb97

Browse files
committed
Remove support for excluding intermediate resources in nested includes
1 parent 14d4bf7 commit 33dcb97

4 files changed

Lines changed: 15 additions & 246 deletions

File tree

lib/jsonapi/include_directives.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module JSONAPI
22
class IncludeDirectives
33

44
# Construct an IncludeDirectives Hash from an array of dot separated include strings.
5-
# For example [:posts, 'posts.comments', 'posts.comments.tags']
5+
# For example ['posts.comments.tags']
66
# will transform into =>
77
# {
88
# :posts=>{
@@ -55,13 +55,12 @@ def get_includes(directive)
5555

5656
def parse_include(include)
5757
parts = include.split('.')
58-
local_path = ''
59-
parts.each_with_index do |part, index|
60-
local_path += local_path.length > 0 ? ".#{part}" : part
58+
local_path = ''
59+
60+
parts.each do |name|
61+
local_path += local_path.length > 0 ? ".#{name}" : name
6162
related = get_related(local_path)
62-
if index == parts.length - 1
63-
related[:include] = true
64-
end
63+
related[:include] = true
6564
end
6665
end
6766
end

test/controllers/controller_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,10 +1437,10 @@ def test_show_has_one_relationship_nil
14371437

14381438
class TagsControllerTest < ActionController::TestCase
14391439
def test_tags_index
1440-
get :index, {filter: {id: '6,7,8,9'}, include: 'posts,posts.tags,posts.author.posts'}
1440+
get :index, {filter: {id: '6,7,8,9'}, include: 'posts.tags,posts.author.posts'}
14411441
assert_response :success
14421442
assert_equal 4, json_response['data'].size
1443-
assert_equal 2, json_response['included'].size
1443+
assert_equal 3, json_response['included'].size
14441444
end
14451445

14461446
def test_tags_show_multiple
@@ -1450,7 +1450,7 @@ def test_tags_show_multiple
14501450
end
14511451

14521452
def test_tags_show_multiple_with_include
1453-
get :show, {id: '6,7,8,9', include: 'posts,posts.tags,posts.author.posts'}
1453+
get :show, {id: '6,7,8,9', include: 'posts.tags,posts.author.posts'}
14541454
assert_response :bad_request
14551455
assert_match /6,7,8,9 is not a valid value for id/, response.body
14561456
end

test/unit/serializer/include_directives_test.rb

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_one_level_multiple_includes
4141
directives)
4242
end
4343

44-
def test_two_levels_include_full
45-
directives = JSONAPI::IncludeDirectives.new(['posts', 'posts.comments']).include_directives
44+
def test_two_levels_include_full_path
45+
directives = JSONAPI::IncludeDirectives.new(['posts.comments']).include_directives
4646

4747
assert_hash_equals(
4848
{
@@ -61,14 +61,14 @@ def test_two_levels_include_full
6161
directives)
6262
end
6363

64-
def test_two_levels_include_lowest_only
65-
directives = JSONAPI::IncludeDirectives.new(['posts.comments']).include_directives
64+
def test_two_levels_include_full_path_redundant
65+
directives = JSONAPI::IncludeDirectives.new(['posts','posts.comments']).include_directives
6666

6767
assert_hash_equals(
6868
{
6969
include_related: {
7070
posts: {
71-
include: false,
71+
include: true,
7272
include_related:{
7373
comments: {
7474
include: true,
@@ -82,7 +82,7 @@ def test_two_levels_include_lowest_only
8282
end
8383

8484
def test_three_levels_include_full
85-
directives = JSONAPI::IncludeDirectives.new(['posts', 'posts.comments', 'posts.comments.tags']).include_directives
85+
directives = JSONAPI::IncludeDirectives.new(['posts.comments.tags']).include_directives
8686

8787
assert_hash_equals(
8888
{
@@ -105,33 +105,4 @@ def test_three_levels_include_full
105105
},
106106
directives)
107107
end
108-
109-
def test_three_levels_skip_middle
110-
directives = JSONAPI::IncludeDirectives.new(['posts', 'tags', 'posts.comments.tags']).include_directives
111-
112-
assert_hash_equals(
113-
{
114-
include_related: {
115-
tags: {
116-
include: true,
117-
include_related:{}
118-
},
119-
posts: {
120-
include: true,
121-
include_related:{
122-
comments: {
123-
include: false,
124-
include_related:{
125-
tags: {
126-
include: true,
127-
include_related:{}
128-
}
129-
}
130-
}
131-
}
132-
}
133-
}
134-
},
135-
directives)
136-
end
137108
end

test/unit/serializer/serializer_test.rb

Lines changed: 0 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -562,207 +562,6 @@ def test_serializer_include_sub_objects
562562
)
563563
end
564564

565-
def test_serializer_include_has_many_sub_objects_only
566-
567-
assert_hash_equals(
568-
{
569-
data: {
570-
type: 'posts',
571-
id: '1',
572-
attributes: {
573-
title: 'New post',
574-
body: 'A body!!!',
575-
subject: 'New post'
576-
},
577-
links: {
578-
self: '/posts/1'
579-
},
580-
relationships: {
581-
section: {
582-
links: {
583-
self: '/posts/1/relationships/section',
584-
related: '/posts/1/section'
585-
},
586-
data: nil
587-
},
588-
author: {
589-
links: {
590-
self: '/posts/1/relationships/author',
591-
related: '/posts/1/author'
592-
},
593-
data: {
594-
type: 'people',
595-
id: '1'
596-
}
597-
},
598-
tags: {
599-
links: {
600-
self: '/posts/1/relationships/tags',
601-
related: '/posts/1/tags'
602-
}
603-
},
604-
comments: {
605-
links: {
606-
self: '/posts/1/relationships/comments',
607-
related: '/posts/1/comments'
608-
}
609-
}
610-
}
611-
},
612-
included: [
613-
{
614-
type: 'tags',
615-
id: '1',
616-
attributes: {
617-
name: 'short'
618-
},
619-
links: {
620-
self: '/tags/1'
621-
},
622-
relationships: {
623-
posts: {
624-
links: {
625-
self: '/tags/1/relationships/posts',
626-
related: '/tags/1/posts'
627-
}
628-
}
629-
}
630-
},
631-
{
632-
type: 'tags',
633-
id: '2',
634-
attributes: {
635-
name: 'whiny'
636-
},
637-
links: {
638-
self: '/tags/2'
639-
},
640-
relationships: {
641-
posts: {
642-
links: {
643-
self: '/tags/2/relationships/posts',
644-
related: '/tags/2/posts'
645-
}
646-
}
647-
}
648-
},
649-
{
650-
type: 'tags',
651-
id: '4',
652-
attributes: {
653-
name: 'happy'
654-
},
655-
links: {
656-
self: '/tags/4'
657-
},
658-
relationships: {
659-
posts: {
660-
links: {
661-
self: '/tags/4/relationships/posts',
662-
related: '/tags/4/posts'
663-
}
664-
}
665-
}
666-
}
667-
]
668-
},
669-
JSONAPI::ResourceSerializer.new(PostResource, include: ['comments.tags']).serialize_to_hash(PostResource.new(@post))
670-
)
671-
end
672-
673-
def test_serializer_include_has_one_sub_objects_only
674-
675-
assert_hash_equals(
676-
{
677-
data: {
678-
type: 'posts',
679-
id: '1',
680-
attributes: {
681-
title: 'New post',
682-
body: 'A body!!!',
683-
subject: 'New post'
684-
},
685-
links: {
686-
self: '/posts/1'
687-
},
688-
relationships: {
689-
section: {
690-
links: {
691-
self: '/posts/1/relationships/section',
692-
related: '/posts/1/section'
693-
},
694-
data: nil
695-
},
696-
author: {
697-
links: {
698-
self: '/posts/1/relationships/author',
699-
related: '/posts/1/author'
700-
},
701-
data: {
702-
type: 'people',
703-
id: '1'
704-
}
705-
},
706-
tags: {
707-
links: {
708-
self: '/posts/1/relationships/tags',
709-
related: '/posts/1/tags'
710-
}
711-
},
712-
comments: {
713-
links: {
714-
self: '/posts/1/relationships/comments',
715-
related: '/posts/1/comments'
716-
}
717-
}
718-
}
719-
},
720-
included: [
721-
{
722-
type: 'comments',
723-
id: '1',
724-
attributes: {
725-
body: 'what a dumb post'
726-
},
727-
links: {
728-
self: '/comments/1'
729-
},
730-
relationships: {
731-
author: {
732-
links: {
733-
self: '/comments/1/relationships/author',
734-
related: '/comments/1/author'
735-
},
736-
data: {
737-
type: 'people',
738-
id: '1'
739-
}
740-
},
741-
post: {
742-
links: {
743-
self: '/comments/1/relationships/post',
744-
related: '/comments/1/post'
745-
},
746-
data: {
747-
type: 'posts',
748-
id: '1'
749-
}
750-
},
751-
tags: {
752-
links: {
753-
self: '/comments/1/relationships/tags',
754-
related: '/comments/1/tags'
755-
}
756-
}
757-
}
758-
}
759-
]
760-
},
761-
JSONAPI::ResourceSerializer.new(PostResource,
762-
include: ['author.comments']).serialize_to_hash(PostResource.new(@post))
763-
)
764-
end
765-
766565
def test_serializer_different_foreign_key
767566
serialized = JSONAPI::ResourceSerializer.new(
768567
PersonResource,

0 commit comments

Comments
 (0)