jsonapi-resources 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/README.md +186 -45
- data/lib/jsonapi/association.rb +6 -20
- data/lib/jsonapi/configuration.rb +27 -0
- data/lib/jsonapi/error_codes.rb +2 -0
- data/lib/jsonapi/exceptions.rb +41 -0
- data/lib/jsonapi/formatter.rb +94 -0
- data/lib/jsonapi/operation.rb +46 -9
- data/lib/jsonapi/request.rb +178 -48
- data/lib/jsonapi/resource.rb +51 -84
- data/lib/jsonapi/resource_controller.rb +53 -25
- data/lib/jsonapi/resource_for.rb +4 -6
- data/lib/jsonapi/resource_serializer.rb +59 -31
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/routing_ext.rb +9 -1
- data/lib/jsonapi-resources.rb +2 -0
- data/test/controllers/controller_test.rb +706 -335
- data/test/fixtures/active_record.rb +60 -24
- data/test/integration/requests/request_test.rb +10 -15
- data/test/integration/routes/routes_test.rb +68 -20
- data/test/test_helper.rb +45 -3
- data/test/unit/operation/operations_processor_test.rb +49 -7
- data/test/unit/resource/resource_test.rb +2 -2
- data/test/unit/serializer/serializer_test.rb +483 -360
- metadata +5 -2
|
@@ -1,429 +1,552 @@
|
|
|
1
1
|
require File.expand_path('../../../test_helper', __FILE__)
|
|
2
2
|
require File.expand_path('../../../fixtures/active_record', __FILE__)
|
|
3
|
-
require 'jsonapi
|
|
4
|
-
|
|
3
|
+
require 'jsonapi-resources'
|
|
4
|
+
require 'json'
|
|
5
5
|
|
|
6
6
|
class SerializerTest < MiniTest::Unit::TestCase
|
|
7
7
|
def setup
|
|
8
8
|
@post = Post.find(1)
|
|
9
9
|
@fred = Person.find_by(name: 'Fred Reader')
|
|
10
|
+
|
|
11
|
+
@expense_entry = ExpenseEntry.find(1)
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def test_serializer
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
|
|
16
|
+
assert_hash_equals(
|
|
17
|
+
{
|
|
18
|
+
posts: {
|
|
19
|
+
id: 1,
|
|
20
|
+
title: 'New post',
|
|
21
|
+
body: 'A body!!!',
|
|
22
|
+
subject: 'New post',
|
|
23
|
+
links: {
|
|
24
|
+
section: nil,
|
|
25
|
+
author: 1,
|
|
26
|
+
tags: [1, 2, 3],
|
|
27
|
+
comments: [1, 2]
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
32
|
+
PostResource.new(@post)))
|
|
27
33
|
end
|
|
28
34
|
|
|
29
35
|
def test_serializer_limited_fieldset
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
|
|
37
|
+
assert_hash_equals(
|
|
38
|
+
{
|
|
39
|
+
posts: {
|
|
40
|
+
id: 1,
|
|
41
|
+
title: 'New post',
|
|
42
|
+
links: {
|
|
43
|
+
author: 1
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
48
|
+
PostResource.new(@post),
|
|
49
|
+
fields: {posts: [:id, :title, :author]}))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_serializer_include
|
|
53
|
+
|
|
54
|
+
assert_hash_equals(
|
|
55
|
+
{
|
|
56
|
+
posts: {
|
|
57
|
+
id: 1,
|
|
58
|
+
title: 'New post',
|
|
59
|
+
body: 'A body!!!',
|
|
60
|
+
subject: 'New post',
|
|
61
|
+
links: {
|
|
62
|
+
author: 1,
|
|
63
|
+
tags: [1, 2, 3],
|
|
64
|
+
comments: [1, 2],
|
|
65
|
+
section: nil
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
linked: {
|
|
69
|
+
people: [{
|
|
32
70
|
id: 1,
|
|
33
|
-
|
|
71
|
+
name: 'Joe Author',
|
|
72
|
+
email: 'joe@xyz.fake',
|
|
73
|
+
dateJoined: '2013-08-07 16:25:00 -0400',
|
|
34
74
|
links: {
|
|
35
|
-
|
|
75
|
+
comments: [1],
|
|
76
|
+
posts: [1, 2, 11]
|
|
36
77
|
}
|
|
37
78
|
}]
|
|
38
|
-
|
|
39
|
-
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
82
|
+
PostResource.new(@post), include: [:author]))
|
|
40
83
|
end
|
|
41
84
|
|
|
42
|
-
def
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
85
|
+
def test_serializer_key_format
|
|
86
|
+
|
|
87
|
+
assert_hash_equals(
|
|
88
|
+
{
|
|
89
|
+
posts: {
|
|
90
|
+
id: 1,
|
|
91
|
+
title: 'New post',
|
|
92
|
+
body: 'A body!!!',
|
|
93
|
+
subject: 'New post',
|
|
94
|
+
links: {
|
|
95
|
+
author: 1,
|
|
96
|
+
tags: [1, 2, 3],
|
|
97
|
+
comments: [1, 2],
|
|
98
|
+
section: nil
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
linked: {
|
|
102
|
+
people: [{
|
|
103
|
+
id: 1,
|
|
104
|
+
name: 'Joe Author',
|
|
105
|
+
email: 'joe@xyz.fake',
|
|
106
|
+
date_joined: '2013-08-07 16:25:00 -0400',
|
|
107
|
+
links: {
|
|
108
|
+
comments: [1],
|
|
109
|
+
posts: [1, 2, 11]
|
|
110
|
+
}
|
|
111
|
+
}]
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
115
|
+
PostResource.new(@post),
|
|
116
|
+
include: [:author],
|
|
117
|
+
key_formatter: UnderscoredKeyFormatter))
|
|
69
118
|
end
|
|
70
119
|
|
|
71
120
|
def test_serializer_include_sub_objects
|
|
121
|
+
|
|
72
122
|
assert_hash_equals(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
}],
|
|
86
|
-
linked: {
|
|
87
|
-
tags: [
|
|
88
|
-
{
|
|
89
|
-
id: 1,
|
|
90
|
-
name: 'short',
|
|
91
|
-
links: {
|
|
92
|
-
posts: :not_nil
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
id: 2,
|
|
97
|
-
name: 'whiny',
|
|
98
|
-
links: {
|
|
99
|
-
posts: :not_nil
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
id: 4,
|
|
104
|
-
name: 'happy',
|
|
105
|
-
links: {
|
|
106
|
-
posts: :not_nil
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
],
|
|
110
|
-
comments: [
|
|
111
|
-
{
|
|
112
|
-
id: 1,
|
|
113
|
-
body: 'what a dumb post',
|
|
114
|
-
links: {
|
|
115
|
-
author: 1,
|
|
116
|
-
post: 1,
|
|
117
|
-
tags: [2, 1]
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
id: 2,
|
|
122
|
-
body: 'i liked it',
|
|
123
|
-
links: {
|
|
124
|
-
author: 2,
|
|
125
|
-
post: 1,
|
|
126
|
-
tags: [4, 1]
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
]
|
|
123
|
+
{
|
|
124
|
+
posts: {
|
|
125
|
+
id: 1,
|
|
126
|
+
title: 'New post',
|
|
127
|
+
body: 'A body!!!',
|
|
128
|
+
subject: 'New post',
|
|
129
|
+
links: {
|
|
130
|
+
author: 1,
|
|
131
|
+
tags: [1, 2, 3],
|
|
132
|
+
comments: [1, 2],
|
|
133
|
+
section: nil
|
|
130
134
|
}
|
|
131
|
-
|
|
135
|
+
},
|
|
136
|
+
linked: {
|
|
137
|
+
tags: [
|
|
138
|
+
{
|
|
139
|
+
id: 1,
|
|
140
|
+
name: 'short',
|
|
141
|
+
links: {
|
|
142
|
+
posts: :not_nil
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 2,
|
|
147
|
+
name: 'whiny',
|
|
148
|
+
links: {
|
|
149
|
+
posts: :not_nil
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
id: 4,
|
|
154
|
+
name: 'happy',
|
|
155
|
+
links: {
|
|
156
|
+
posts: :not_nil
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
],
|
|
160
|
+
comments: [
|
|
161
|
+
{
|
|
162
|
+
id: 1,
|
|
163
|
+
body: 'what a dumb post',
|
|
164
|
+
links: {
|
|
165
|
+
author: 1,
|
|
166
|
+
post: 1,
|
|
167
|
+
tags: [2, 1]
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
id: 2,
|
|
172
|
+
body: 'i liked it',
|
|
173
|
+
links: {
|
|
174
|
+
author: 2,
|
|
175
|
+
post: 1,
|
|
176
|
+
tags: [4, 1]
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
183
|
+
PostResource.new(@post), include: [:comments, 'comments.tags']))
|
|
132
184
|
end
|
|
133
185
|
|
|
134
186
|
def test_serializer_include_has_many_sub_objects_only
|
|
187
|
+
|
|
135
188
|
assert_hash_equals(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
}],
|
|
149
|
-
linked: {
|
|
150
|
-
tags: [
|
|
151
|
-
{
|
|
152
|
-
id: 1,
|
|
153
|
-
name: 'short',
|
|
154
|
-
links: {
|
|
155
|
-
posts: :not_nil
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
id: 2,
|
|
160
|
-
name: 'whiny',
|
|
161
|
-
links: {
|
|
162
|
-
posts: :not_nil
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
id: 4,
|
|
167
|
-
name: 'happy',
|
|
168
|
-
links: {
|
|
169
|
-
posts: :not_nil
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
]
|
|
189
|
+
{
|
|
190
|
+
posts: {
|
|
191
|
+
id: 1,
|
|
192
|
+
title: 'New post',
|
|
193
|
+
body: 'A body!!!',
|
|
194
|
+
subject: 'New post',
|
|
195
|
+
links: {
|
|
196
|
+
author: 1,
|
|
197
|
+
tags: [1, 2, 3],
|
|
198
|
+
comments: [1, 2],
|
|
199
|
+
section: nil
|
|
173
200
|
}
|
|
174
|
-
|
|
201
|
+
},
|
|
202
|
+
linked: {
|
|
203
|
+
tags: [
|
|
204
|
+
{
|
|
205
|
+
id: 1,
|
|
206
|
+
name: 'short',
|
|
207
|
+
links: {
|
|
208
|
+
posts: :not_nil
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
id: 2,
|
|
213
|
+
name: 'whiny',
|
|
214
|
+
links: {
|
|
215
|
+
posts: :not_nil
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
id: 4,
|
|
220
|
+
name: 'happy',
|
|
221
|
+
links: {
|
|
222
|
+
posts: :not_nil
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
229
|
+
PostResource.new(@post), include: ['comments.tags']))
|
|
175
230
|
end
|
|
176
231
|
|
|
177
232
|
def test_serializer_include_has_one_sub_objects_only
|
|
233
|
+
|
|
178
234
|
assert_hash_equals(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
}],
|
|
192
|
-
linked: {
|
|
193
|
-
comments: [
|
|
194
|
-
{
|
|
195
|
-
id: 1,
|
|
196
|
-
body: 'what a dumb post',
|
|
197
|
-
links: {
|
|
198
|
-
author: 1,
|
|
199
|
-
post: 1,
|
|
200
|
-
tags: [2, 1]
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
]
|
|
235
|
+
{
|
|
236
|
+
posts: {
|
|
237
|
+
id: 1,
|
|
238
|
+
title: 'New post',
|
|
239
|
+
body: 'A body!!!',
|
|
240
|
+
subject: 'New post',
|
|
241
|
+
links: {
|
|
242
|
+
author: 1,
|
|
243
|
+
tags: [1, 2, 3],
|
|
244
|
+
comments: [1, 2],
|
|
245
|
+
section: nil
|
|
204
246
|
}
|
|
205
|
-
|
|
247
|
+
},
|
|
248
|
+
linked: {
|
|
249
|
+
comments: [
|
|
250
|
+
{
|
|
251
|
+
id: 1,
|
|
252
|
+
body: 'what a dumb post',
|
|
253
|
+
links: {
|
|
254
|
+
author: 1,
|
|
255
|
+
post: 1,
|
|
256
|
+
tags: [2, 1]
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
]
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
263
|
+
PostResource.new(@post), include: ['author.comments']))
|
|
206
264
|
end
|
|
207
265
|
|
|
208
266
|
def test_serializer_different_foreign_key
|
|
267
|
+
|
|
209
268
|
assert_hash_equals(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
}],
|
|
221
|
-
linked: {
|
|
222
|
-
comments: [{
|
|
223
|
-
id: 2,
|
|
224
|
-
body: 'i liked it',
|
|
225
|
-
links: {
|
|
226
|
-
author: 2,
|
|
227
|
-
post: 1,
|
|
228
|
-
tags: [4, 1]
|
|
229
|
-
}
|
|
230
|
-
},
|
|
231
|
-
{
|
|
232
|
-
id: 3,
|
|
233
|
-
body: 'Thanks man. Great post. But what is JR?',
|
|
234
|
-
links: {
|
|
235
|
-
author: 2,
|
|
236
|
-
post: 2,
|
|
237
|
-
tags: [5]
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
]
|
|
269
|
+
{
|
|
270
|
+
people: {
|
|
271
|
+
id: 2,
|
|
272
|
+
name: 'Fred Reader',
|
|
273
|
+
email: 'fred@xyz.fake',
|
|
274
|
+
dateJoined: '2013-10-31 16:25:00 -0400',
|
|
275
|
+
links: {
|
|
276
|
+
posts: [],
|
|
277
|
+
comments: [2, 3]
|
|
241
278
|
}
|
|
242
|
-
|
|
279
|
+
},
|
|
280
|
+
linked: {
|
|
281
|
+
comments: [{
|
|
282
|
+
id: 2,
|
|
283
|
+
body: 'i liked it',
|
|
284
|
+
links: {
|
|
285
|
+
author: 2,
|
|
286
|
+
post: 1,
|
|
287
|
+
tags: [4, 1]
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
id: 3,
|
|
292
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
293
|
+
links: {
|
|
294
|
+
author: 2,
|
|
295
|
+
post: 2,
|
|
296
|
+
tags: [5]
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
]
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
303
|
+
PersonResource.new(@fred), include: ['comments']))
|
|
243
304
|
end
|
|
244
305
|
|
|
245
306
|
def test_serializer_array_of_resources
|
|
246
307
|
|
|
247
308
|
posts = []
|
|
248
|
-
Post.find(1,2).each do |post|
|
|
309
|
+
Post.find(1, 2).each do |post|
|
|
249
310
|
posts.push PostResource.new(post)
|
|
250
311
|
end
|
|
251
312
|
|
|
252
313
|
assert_hash_equals(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
id: 1,
|
|
256
|
-
title: 'New post',
|
|
257
|
-
body: 'A body!!!',
|
|
258
|
-
subject: 'New post',
|
|
259
|
-
links: {
|
|
260
|
-
author: 1,
|
|
261
|
-
tags: [1,2,3],
|
|
262
|
-
comments: [1,2],
|
|
263
|
-
section: nil
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
{
|
|
267
|
-
id: 2,
|
|
268
|
-
title: 'JR Solves your serialization woes!',
|
|
269
|
-
body: 'Use JR',
|
|
270
|
-
subject: 'JR Solves your serialization woes!',
|
|
271
|
-
links: {
|
|
272
|
-
author: 1,
|
|
273
|
-
tags: [5],
|
|
274
|
-
comments: [3],
|
|
275
|
-
section: 3
|
|
276
|
-
}
|
|
277
|
-
}],
|
|
278
|
-
linked: {
|
|
279
|
-
tags: [
|
|
280
|
-
{
|
|
314
|
+
{
|
|
315
|
+
posts: [{
|
|
281
316
|
id: 1,
|
|
282
|
-
|
|
317
|
+
title: 'New post',
|
|
318
|
+
body: 'A body!!!',
|
|
319
|
+
subject: 'New post',
|
|
283
320
|
links: {
|
|
284
|
-
|
|
321
|
+
author: 1,
|
|
322
|
+
tags: [1, 2, 3],
|
|
323
|
+
comments: [1, 2],
|
|
324
|
+
section: nil
|
|
285
325
|
}
|
|
286
326
|
},
|
|
287
327
|
{
|
|
288
328
|
id: 2,
|
|
289
|
-
|
|
329
|
+
title: 'JR Solves your serialization woes!',
|
|
330
|
+
body: 'Use JR',
|
|
331
|
+
subject: 'JR Solves your serialization woes!',
|
|
290
332
|
links: {
|
|
291
|
-
|
|
333
|
+
author: 1,
|
|
334
|
+
tags: [5],
|
|
335
|
+
comments: [3],
|
|
336
|
+
section: 3
|
|
292
337
|
}
|
|
293
|
-
},
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
338
|
+
}],
|
|
339
|
+
linked: {
|
|
340
|
+
tags: [
|
|
341
|
+
{
|
|
342
|
+
id: 1,
|
|
343
|
+
name: 'short',
|
|
344
|
+
links: {
|
|
345
|
+
posts: :not_nil
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
id: 2,
|
|
350
|
+
name: 'whiny',
|
|
351
|
+
links: {
|
|
352
|
+
posts: :not_nil
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
id: 4,
|
|
357
|
+
name: 'happy',
|
|
358
|
+
links: {
|
|
359
|
+
posts: :not_nil
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
{
|
|
363
|
+
id: 5,
|
|
364
|
+
name: 'JR',
|
|
365
|
+
links: {
|
|
366
|
+
posts: [2, 11]
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
],
|
|
370
|
+
comments: [
|
|
371
|
+
{
|
|
372
|
+
id: 1,
|
|
373
|
+
body: 'what a dumb post',
|
|
374
|
+
links: {
|
|
375
|
+
author: 1,
|
|
376
|
+
post: 1,
|
|
377
|
+
tags: [2, 1]
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
id: 2,
|
|
382
|
+
body: 'i liked it',
|
|
383
|
+
links: {
|
|
384
|
+
author: 2,
|
|
385
|
+
post: 1,
|
|
386
|
+
tags: [4, 1]
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
{
|
|
390
|
+
id: 3,
|
|
391
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
392
|
+
links: {
|
|
393
|
+
author: 2,
|
|
394
|
+
post: 2,
|
|
395
|
+
tags: [5]
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
]
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
402
|
+
posts, include: ['comments', 'comments.tags']))
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
def test_serializer_array_of_resources_limited_fields
|
|
406
|
+
|
|
407
|
+
posts = []
|
|
408
|
+
Post.find(1, 2).each do |post|
|
|
409
|
+
posts.push PostResource.new(post)
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
assert_hash_equals(
|
|
413
|
+
{
|
|
414
|
+
posts: [{
|
|
415
|
+
id: 1,
|
|
416
|
+
title: 'New post',
|
|
297
417
|
links: {
|
|
298
|
-
|
|
418
|
+
author: 1
|
|
299
419
|
}
|
|
300
420
|
},
|
|
301
421
|
{
|
|
302
|
-
id:
|
|
303
|
-
|
|
422
|
+
id: 2,
|
|
423
|
+
title: 'JR Solves your serialization woes!',
|
|
304
424
|
links: {
|
|
305
|
-
|
|
425
|
+
author: 1
|
|
306
426
|
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
427
|
+
}],
|
|
428
|
+
linked: {
|
|
429
|
+
tags: [
|
|
430
|
+
{
|
|
431
|
+
name: 'short'
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
name: 'whiny'
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
name: 'happy'
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: 'JR'
|
|
441
|
+
}
|
|
442
|
+
],
|
|
443
|
+
comments: [
|
|
444
|
+
{
|
|
445
|
+
id: 1,
|
|
446
|
+
body: 'what a dumb post',
|
|
447
|
+
links: {
|
|
448
|
+
post: 1
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
id: 2,
|
|
453
|
+
body: 'i liked it',
|
|
454
|
+
links: {
|
|
455
|
+
post: 1
|
|
456
|
+
}
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
id: 3,
|
|
460
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
461
|
+
links: {
|
|
462
|
+
post: 2
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
],
|
|
466
|
+
posts: [
|
|
467
|
+
{
|
|
468
|
+
id: 11,
|
|
469
|
+
title: 'JR How To',
|
|
470
|
+
links: {
|
|
471
|
+
author: 1
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
],
|
|
475
|
+
people: [
|
|
476
|
+
{
|
|
477
|
+
id: 1,
|
|
478
|
+
email: 'joe@xyz.fake',
|
|
479
|
+
links: {
|
|
480
|
+
comments: [1]
|
|
481
|
+
}
|
|
482
|
+
}]
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
486
|
+
posts,
|
|
487
|
+
include: ['comments', 'author', 'comments.tags', 'author.posts'],
|
|
488
|
+
fields: {
|
|
489
|
+
people: [:id, :email, :comments],
|
|
490
|
+
posts: [:id, :title, :author],
|
|
491
|
+
tags: [:name],
|
|
492
|
+
comments: [:id, :body, :post]
|
|
493
|
+
}))
|
|
340
494
|
end
|
|
341
495
|
|
|
342
|
-
def
|
|
496
|
+
def test_serializer_camelized_with_value_formatters
|
|
497
|
+
assert_hash_equals(
|
|
498
|
+
{
|
|
499
|
+
expenseEntries: {
|
|
500
|
+
id: 1,
|
|
501
|
+
transactionDate: '04/15/2014',
|
|
502
|
+
cost: '12.05',
|
|
503
|
+
links: {
|
|
504
|
+
isoCurrency: 'USD',
|
|
505
|
+
employee: 3
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
linked: {
|
|
509
|
+
isoCurrencies: [{
|
|
510
|
+
code: 'USD',
|
|
511
|
+
countryName: 'United States',
|
|
512
|
+
name: 'United States Dollar',
|
|
513
|
+
minorUnit: 'cent'
|
|
514
|
+
}],
|
|
515
|
+
people: [{
|
|
516
|
+
id: 3,
|
|
517
|
+
name: 'Lazy Author',
|
|
518
|
+
email: 'lazy@xyz.fake',
|
|
519
|
+
dateJoined: '2013-10-31 17:25:00 -0400',
|
|
520
|
+
}]
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
JSONAPI::ResourceSerializer.new.serialize_to_hash(
|
|
524
|
+
ExpenseEntryResource.new(@expense_entry),
|
|
525
|
+
include: ['iso_currency', 'employee'],
|
|
526
|
+
fields: {people: [:id, :name, :email, :date_joined]}
|
|
527
|
+
)
|
|
528
|
+
)
|
|
529
|
+
end
|
|
343
530
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
posts.push PostResource.new(post)
|
|
347
|
-
end
|
|
531
|
+
def test_serializer_empty_links_null_and_array
|
|
532
|
+
planet_hash = JSONAPI::ResourceSerializer.new.serialize_to_hash(PlanetResource.new(Planet.find(8)))
|
|
348
533
|
|
|
349
534
|
assert_hash_equals(
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
tags: [
|
|
367
|
-
{
|
|
368
|
-
name: 'short'
|
|
369
|
-
},
|
|
370
|
-
{
|
|
371
|
-
name: 'whiny'
|
|
372
|
-
},
|
|
373
|
-
{
|
|
374
|
-
name: 'happy'
|
|
375
|
-
},
|
|
376
|
-
{
|
|
377
|
-
name: 'JR'
|
|
378
|
-
}
|
|
379
|
-
],
|
|
380
|
-
comments: [
|
|
381
|
-
{
|
|
382
|
-
id: 1,
|
|
383
|
-
body: 'what a dumb post',
|
|
384
|
-
links: {
|
|
385
|
-
post: 1
|
|
386
|
-
}
|
|
387
|
-
},
|
|
388
|
-
{
|
|
389
|
-
id: 2,
|
|
390
|
-
body: 'i liked it',
|
|
391
|
-
links: {
|
|
392
|
-
post: 1
|
|
393
|
-
}
|
|
394
|
-
},
|
|
395
|
-
{
|
|
396
|
-
id: 3,
|
|
397
|
-
body: 'Thanks man. Great post. But what is JR?',
|
|
398
|
-
links: {
|
|
399
|
-
post: 2
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
],
|
|
403
|
-
posts: [
|
|
404
|
-
{
|
|
405
|
-
id: 11,
|
|
406
|
-
title: 'JR How To',
|
|
407
|
-
links: {
|
|
408
|
-
author: 1
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
],
|
|
412
|
-
people: [
|
|
413
|
-
{
|
|
414
|
-
id: 1,
|
|
415
|
-
email: 'joe@xyz.fake',
|
|
416
|
-
links: {
|
|
417
|
-
comments: [1]
|
|
418
|
-
}
|
|
419
|
-
}]
|
|
420
|
-
}
|
|
421
|
-
}, JSONAPI::ResourceSerializer.new.serialize(posts, ['comments','author','comments.tags','author.posts'],
|
|
422
|
-
{
|
|
423
|
-
people: [:id, :email, :comments],
|
|
424
|
-
posts: [:id, :title, :author],
|
|
425
|
-
tags: [:name],
|
|
426
|
-
comments: [:id, :body, :post]
|
|
427
|
-
}))
|
|
535
|
+
{
|
|
536
|
+
planets: {
|
|
537
|
+
id: 8,
|
|
538
|
+
name: 'Beta W',
|
|
539
|
+
description: 'Newly discovered Planet W',
|
|
540
|
+
links: {
|
|
541
|
+
planetType: nil,
|
|
542
|
+
tags: [],
|
|
543
|
+
moons: []
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}, planet_hash)
|
|
547
|
+
|
|
548
|
+
json = planet_hash.to_json
|
|
549
|
+
assert_match /\"planetType\":null/, json
|
|
550
|
+
assert_match /\"moons\":\[\]/, json
|
|
428
551
|
end
|
|
429
552
|
end
|