@@ -39,7 +39,7 @@ def setup
39
39
@bio2 . author = @author2
40
40
end
41
41
42
- def test_include_multiple_posts_and_linked
42
+ def test_include_multiple_posts_and_linked_array
43
43
serializer = ArraySerializer . new ( [ @first_post , @second_post ] )
44
44
adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new (
45
45
serializer ,
@@ -51,80 +51,80 @@ def test_include_multiple_posts_and_linked
51
51
)
52
52
53
53
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 : [
108
55
{
109
56
id : "10" ,
110
57
title : "Hello!!" ,
111
58
body : "Hello, world!!" ,
59
+ type : "posts" ,
112
60
links : {
113
61
comments : { linkage : [ { type : "comments" , id : '1' } , { type : "comments" , id : '2' } ] } ,
114
62
blog : { linkage : { type : "blogs" , id : "999" } } ,
115
63
author : { linkage : { type : "authors" , id : "1" } }
116
64
}
117
65
} ,
118
66
{
119
- id : "2 " ,
67
+ id : "20 " ,
120
68
title : "New Post" ,
121
69
body : "Body" ,
70
+ type : "posts" ,
122
71
links : {
123
72
comments : { linkage : [ ] } ,
124
73
blog : { linkage : { type : "blogs" , id : "999" } } ,
125
74
author : { linkage : { type : "authors" , id : "2" } }
126
75
}
127
76
}
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
+ }
128
128
]
129
129
}
130
130
assert_equal expected , adapter . serializable_hash
@@ -195,6 +195,36 @@ def test_ignore_model_namespace_for_linked_resource_type
195
195
}
196
196
assert_equal expected , links
197
197
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
198
228
end
199
229
end
200
230
end
0 commit comments