Skip to content

Commit 4891c22

Browse files
committed
Merge pull request #858 from mateomurphy/adapter-fixes
Included resource fixes
2 parents db788a5 + 9aebc6c commit 4891c22

File tree

2 files changed

+97
-62
lines changed

2 files changed

+97
-62
lines changed

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ class JsonApi < Adapter
55
def initialize(serializer, options = {})
66
super
77
serializer.root = true
8-
@hash = {}
9-
@top = @options.fetch(:top) { @hash }
8+
@hash = { data: [] }
109

1110
if fields = options.delete(:fields)
1211
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
@@ -17,8 +16,14 @@ def initialize(serializer, options = {})
1716

1817
def serializable_hash(options = {})
1918
if serializer.respond_to?(:each)
20-
@hash[:data] = serializer.map do |s|
21-
self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[:data]
19+
serializer.each do |s|
20+
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
21+
@hash[:data] << result[:data]
22+
23+
if result[:included]
24+
@hash[:included] ||= []
25+
@hash[:included] |= result[:included]
26+
end
2227
end
2328
else
2429
@hash = cached_object do
@@ -53,14 +58,14 @@ def add_included(resource_name, serializers, parent = nil)
5358
resource_path = [parent, resource_name].compact.join('.')
5459

5560
if include_assoc?(resource_path)
56-
@top[:included] ||= []
61+
@hash[:included] ||= []
5762

5863
serializers.each do |serializer|
5964
attrs = attributes_for_serializer(serializer, @options)
6065

6166
add_resource_links(attrs, serializer, add_included: false)
6267

63-
@top[:included].push(attrs) unless @top[:included].include?(attrs)
68+
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
6469
end
6570
end
6671

test/adapter/json_api/linked_test.rb

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def setup
3939
@bio2.author = @author2
4040
end
4141

42-
def test_include_multiple_posts_and_linked
42+
def test_include_multiple_posts_and_linked_array
4343
serializer = ArraySerializer.new([@first_post, @second_post])
4444
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
4545
serializer,
@@ -51,80 +51,80 @@ def test_include_multiple_posts_and_linked
5151
)
5252

5353
expected = {
54-
linked: {
55-
comments: [
56-
{
57-
id: "1",
58-
body: "ZOMG A COMMENT",
59-
links: {
60-
post: { linkage: { type: "posts", id: "1" } },
61-
author: { linkage: nil }
62-
}
63-
}, {
64-
id: "2",
65-
body: "ZOMG ANOTHER COMMENT",
66-
links: {
67-
post: { linkage: { type: "posts", id: "1" } },
68-
author: { linkage: nil }
69-
}
70-
}
71-
],
72-
authors: [
73-
{
74-
id: "1",
75-
name: "Steve K.",
76-
links: {
77-
posts: { linkage: [ { type: "posts", id: "1" }, { type: "posts", id: "3" } ] },
78-
roles: { linkage: [] },
79-
bio: { linkage: { type: "bios", id: "1" } }
80-
}
81-
}, {
82-
id: "2",
83-
name: "Tenderlove",
84-
links: {
85-
posts: { linkage: [ { type: "posts", id:"2" } ] },
86-
roles: { linkage: [] },
87-
bio: { linkage: { type: "bios", id: "2" } }
88-
}
89-
}
90-
],
91-
bios: [
92-
{
93-
id: "1",
94-
content: "AMS Contributor",
95-
links: {
96-
author: { linkage: { type: "authors", id: "1" } }
97-
}
98-
}, {
99-
id: "2",
100-
content: "Rails Contributor",
101-
links: {
102-
author: { linkage: { type: "authors", id: "2" } }
103-
}
104-
}
105-
]
106-
},
107-
posts: [
54+
data: [
10855
{
10956
id: "10",
11057
title: "Hello!!",
11158
body: "Hello, world!!",
59+
type: "posts",
11260
links: {
11361
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
11462
blog: { linkage: { type: "blogs", id: "999" } },
11563
author: { linkage: { type: "authors", id: "1" } }
11664
}
11765
},
11866
{
119-
id: "2",
67+
id: "20",
12068
title: "New Post",
12169
body: "Body",
70+
type: "posts",
12271
links: {
12372
comments: { linkage: [] },
12473
blog: { linkage: { type: "blogs", id: "999" } },
12574
author: { linkage: { type: "authors", id: "2" } }
12675
}
12776
}
77+
],
78+
included: [
79+
{
80+
id: "1",
81+
body: "ZOMG A COMMENT",
82+
type: "comments",
83+
links: {
84+
post: { linkage: { type: "posts", id: "10" } },
85+
author: { linkage: nil }
86+
}
87+
}, {
88+
id: "2",
89+
body: "ZOMG ANOTHER COMMENT",
90+
type: "comments",
91+
links: {
92+
post: { linkage: { type: "posts", id: "10" } },
93+
author: { linkage: nil }
94+
}
95+
}, {
96+
id: "1",
97+
name: "Steve K.",
98+
type: "authors",
99+
links: {
100+
posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
101+
roles: { linkage: [] },
102+
bio: { linkage: { type: "bios", id: "1" } }
103+
}
104+
}, {
105+
id: "1",
106+
content: "AMS Contributor",
107+
type: "bios",
108+
links: {
109+
author: { linkage: { type: "authors", id: "1" } }
110+
}
111+
}, {
112+
id: "2",
113+
name: "Tenderlove",
114+
type: "authors",
115+
links: {
116+
posts: { linkage: [ { type: "posts", id:"20" } ] },
117+
roles: { linkage: [] },
118+
bio: { linkage: { type: "bios", id: "2" } }
119+
}
120+
}, {
121+
id: "2",
122+
content: "Rails Contributor",
123+
type: "bios",
124+
links: {
125+
author: { linkage: { type: "authors", id: "2" } }
126+
}
127+
}
128128
]
129129
}
130130
assert_equal expected, adapter.serializable_hash
@@ -195,6 +195,36 @@ def test_ignore_model_namespace_for_linked_resource_type
195195
}
196196
assert_equal expected, links
197197
end
198+
199+
def test_multiple_references_to_same_resource
200+
serializer = ArraySerializer.new([@first_comment, @second_comment])
201+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
202+
serializer,
203+
include: ['post']
204+
)
205+
206+
expected = [
207+
{
208+
id: "10",
209+
title: "Hello!!",
210+
body: "Hello, world!!",
211+
type: "posts",
212+
links: {
213+
comments: {
214+
linkage: [{type: "comments", id: "1"}, {type: "comments", id: "2"}]
215+
},
216+
blog: {
217+
linkage: {type: "blogs", id: "999"}
218+
},
219+
author: {
220+
linkage: {type: "authors", id: "1"}
221+
}
222+
}
223+
}
224+
]
225+
226+
assert_equal expected, adapter.serializable_hash[:included]
227+
end
198228
end
199229
end
200230
end

0 commit comments

Comments
 (0)