jsonapi-serializers 0.1.1 → 0.1.2
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/README.md +24 -11
- data/lib/jsonapi-serializers/serializer.rb +4 -1
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +15 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9515dd14ef21eec5f8fdd5011fa7e53d544c844
|
4
|
+
data.tar.gz: ae4aab29eb1048a70c4a004edd265007a992e991
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f76752cbe2cdccb810f7ef9e4de4ace4dd97cacc45e841106d40abedd7401dca8b5326650f5c2994286cf9e5d70e9a57609b5cf4e5ac85b8fd92ba25916d0a7b
|
7
|
+
data.tar.gz: cf20e004587f630c21e92a1aa1247a0281df6f97d6d095199abc08f3ee7835ecc4a36bf7cc19d94170eea8409da473ad65ccba3eaf66b5159457e46d0061b501
|
data/README.md
CHANGED
@@ -77,7 +77,8 @@ Returns a hash:
|
|
77
77
|
},
|
78
78
|
"links": {
|
79
79
|
"self": "/posts/1"
|
80
|
-
}
|
80
|
+
},
|
81
|
+
"relationships": {}
|
81
82
|
}
|
82
83
|
}
|
83
84
|
```
|
@@ -102,7 +103,8 @@ Returns:
|
|
102
103
|
},
|
103
104
|
"links": {
|
104
105
|
"self": "/posts/1"
|
105
|
-
}
|
106
|
+
},
|
107
|
+
"relationships": {}
|
106
108
|
},
|
107
109
|
{
|
108
110
|
"id": "2",
|
@@ -113,7 +115,8 @@ Returns:
|
|
113
115
|
},
|
114
116
|
"links": {
|
115
117
|
"self": "/posts/2"
|
116
|
-
}
|
118
|
+
},
|
119
|
+
"relationships": {}
|
117
120
|
}
|
118
121
|
]
|
119
122
|
}
|
@@ -264,7 +267,7 @@ JSONAPI::Serializer.serialize(post, include: ['author', 'comments', 'comments.us
|
|
264
267
|
Returns:
|
265
268
|
|
266
269
|
```json
|
267
|
-
|
270
|
+
{
|
268
271
|
"data": {
|
269
272
|
"id": "1",
|
270
273
|
"type": "posts",
|
@@ -273,7 +276,9 @@ Returns:
|
|
273
276
|
"content": "Your first post"
|
274
277
|
},
|
275
278
|
"links": {
|
276
|
-
"self": "/posts/1"
|
279
|
+
"self": "/posts/1"
|
280
|
+
},
|
281
|
+
"relationships": {
|
277
282
|
"author": {
|
278
283
|
"self": "/posts/1/links/author",
|
279
284
|
"related": "/posts/1/author",
|
@@ -303,7 +308,8 @@ Returns:
|
|
303
308
|
},
|
304
309
|
"links": {
|
305
310
|
"self": "/users/1"
|
306
|
-
}
|
311
|
+
},
|
312
|
+
"relationships": {}
|
307
313
|
},
|
308
314
|
{
|
309
315
|
"id": "1",
|
@@ -312,7 +318,9 @@ Returns:
|
|
312
318
|
"content": "Have no fear, sers, your king is safe."
|
313
319
|
},
|
314
320
|
"links": {
|
315
|
-
"self": "/comments/1"
|
321
|
+
"self": "/comments/1"
|
322
|
+
},
|
323
|
+
"relationships": {
|
316
324
|
"user": {
|
317
325
|
"self": "/comments/1/links/user",
|
318
326
|
"related": "/comments/1/user",
|
@@ -320,6 +328,10 @@ Returns:
|
|
320
328
|
"type": "users",
|
321
329
|
"id": "2"
|
322
330
|
}
|
331
|
+
},
|
332
|
+
"post": {
|
333
|
+
"self": "/comments/1/links/post",
|
334
|
+
"related": "/comments/1/post"
|
323
335
|
}
|
324
336
|
}
|
325
337
|
},
|
@@ -331,19 +343,20 @@ Returns:
|
|
331
343
|
},
|
332
344
|
"links": {
|
333
345
|
"self": "/users/2"
|
334
|
-
}
|
346
|
+
},
|
347
|
+
"relationships": {}
|
335
348
|
}
|
336
349
|
]
|
337
350
|
}
|
338
351
|
```
|
339
352
|
|
340
353
|
Notice a few things:
|
341
|
-
* The [primary data](http://jsonapi.org/format/#document-structure-top-level) now
|
354
|
+
* The [primary data](http://jsonapi.org/format/#document-structure-top-level) relationships now include "linkage" information for each relationship that was included.
|
342
355
|
* The related objects themselves are loaded in the top-level `included` member.
|
343
356
|
* The related objects _also_ include "linkage" information when a deeper relationship is also present in the compound document. This is a very powerful feature of the JSON:API spec, and allows you to deeply link complicated relationships all in the same document and in a single HTTP response. JSONAPI::Serializers automatically includes the correct linkage information for whatever `include` paths you specify. This conforms to this part of the spec:
|
344
357
|
|
345
|
-
> Note:
|
346
|
-
> [JSON:API
|
358
|
+
> Note: Full linkage ensures that included resources are related to either the primary data (which could be resource objects or resource identifier objects) or to each other.
|
359
|
+
> [JSON:API Compound Documents](http://jsonapi.org/format/#document-structure-compound-documents)
|
347
360
|
|
348
361
|
#### Relationship path handling
|
349
362
|
|
@@ -79,7 +79,10 @@ module JSONAPI
|
|
79
79
|
def links
|
80
80
|
data = {}
|
81
81
|
data.merge!({'self' => self_link}) if self_link
|
82
|
+
end
|
82
83
|
|
84
|
+
def relationships
|
85
|
+
data = {}
|
83
86
|
# Merge in data for has_one relationships.
|
84
87
|
has_one_relationships.each do |attribute_name, object|
|
85
88
|
formatted_attribute_name = format_name(attribute_name)
|
@@ -260,7 +263,6 @@ module JSONAPI
|
|
260
263
|
# Given all the primary objects (either the single root object or collection of objects),
|
261
264
|
# recursively search and find related associations that were specified as includes.
|
262
265
|
objects = options[:is_collection] ? objects.to_a : [objects]
|
263
|
-
serializers = []
|
264
266
|
objects.compact.each do |obj|
|
265
267
|
# Use the mutability of relationship_data as the return datastructure to take advantage
|
266
268
|
# of the internal special merging logic.
|
@@ -296,6 +298,7 @@ module JSONAPI
|
|
296
298
|
# http://jsonapi.org/format/#document-structure-resource-objects
|
297
299
|
data.merge!({'attributes' => serializer.attributes}) if !serializer.attributes.nil?
|
298
300
|
data.merge!({'links' => serializer.links}) if !serializer.links.nil?
|
301
|
+
data.merge!({'relationships' => serializer.relationships}) if !serializer.relationships.nil?
|
299
302
|
data.merge!({'meta' => serializer.meta}) if !serializer.meta.nil?
|
300
303
|
data
|
301
304
|
end
|
data/spec/serializer_spec.rb
CHANGED
@@ -25,6 +25,7 @@ describe JSONAPI::Serializer do
|
|
25
25
|
'links' => {
|
26
26
|
'self' => '/posts/1',
|
27
27
|
},
|
28
|
+
'relationships' => {},
|
28
29
|
})
|
29
30
|
end
|
30
31
|
it 'can serialize primary data for a simple object with a long name' do
|
@@ -38,6 +39,8 @@ describe JSONAPI::Serializer do
|
|
38
39
|
},
|
39
40
|
'links' => {
|
40
41
|
'self' => '/long-comments/1',
|
42
|
+
},
|
43
|
+
'relationships' => {
|
41
44
|
'user' => {
|
42
45
|
'self' => '/long-comments/1/links/user',
|
43
46
|
'related' => '/long-comments/1/user',
|
@@ -62,6 +65,7 @@ describe JSONAPI::Serializer do
|
|
62
65
|
'links' => {
|
63
66
|
'self' => '/posts/1',
|
64
67
|
},
|
68
|
+
'relationships' => {},
|
65
69
|
'meta' => {
|
66
70
|
'copyright' => 'Copyright 2015 Example Corp.',
|
67
71
|
'authors' => [
|
@@ -83,6 +87,8 @@ describe JSONAPI::Serializer do
|
|
83
87
|
},
|
84
88
|
'links' => {
|
85
89
|
'self' => '/posts/1',
|
90
|
+
},
|
91
|
+
'relationships' => {
|
86
92
|
# Both to-one and to-many links are present, but neither include linkage:
|
87
93
|
'author' => {
|
88
94
|
'self' => '/posts/1/links/author',
|
@@ -113,6 +119,8 @@ describe JSONAPI::Serializer do
|
|
113
119
|
},
|
114
120
|
'links' => {
|
115
121
|
'self' => '/posts/1',
|
122
|
+
},
|
123
|
+
'relationships' => {
|
116
124
|
'author' => {
|
117
125
|
'self' => '/posts/1/links/author',
|
118
126
|
'related' => '/posts/1/author',
|
@@ -145,6 +153,8 @@ describe JSONAPI::Serializer do
|
|
145
153
|
},
|
146
154
|
'links' => {
|
147
155
|
'self' => '/posts/1',
|
156
|
+
},
|
157
|
+
'relationships' => {
|
148
158
|
'author' => {
|
149
159
|
'self' => '/posts/1/links/author',
|
150
160
|
'related' => '/posts/1/author',
|
@@ -180,6 +190,8 @@ describe JSONAPI::Serializer do
|
|
180
190
|
},
|
181
191
|
'links' => {
|
182
192
|
'self' => '/posts/1',
|
193
|
+
},
|
194
|
+
'relationships' => {
|
183
195
|
'author' => {
|
184
196
|
'self' => '/posts/1/links/author',
|
185
197
|
'related' => '/posts/1/author',
|
@@ -213,6 +225,8 @@ describe JSONAPI::Serializer do
|
|
213
225
|
},
|
214
226
|
'links' => {
|
215
227
|
'self' => '/posts/1',
|
228
|
+
},
|
229
|
+
'relationships' => {
|
216
230
|
'author' => {
|
217
231
|
'self' => '/posts/1/links/author',
|
218
232
|
'related' => '/posts/1/author',
|
@@ -414,7 +428,7 @@ describe JSONAPI::Serializer do
|
|
414
428
|
],
|
415
429
|
}
|
416
430
|
includes = ['long-comments.post.author']
|
417
|
-
actual_data = JSONAPI::Serializer.serialize(post, include:
|
431
|
+
actual_data = JSONAPI::Serializer.serialize(post, include: includes)
|
418
432
|
# Multiple expectations for better diff output for debugging.
|
419
433
|
expect(actual_data['data']).to eq(expected_data['data'])
|
420
434
|
expect(actual_data['included']).to eq(expected_data['included'])
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fotinakis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -132,3 +132,4 @@ test_files:
|
|
132
132
|
- spec/spec_helper.rb
|
133
133
|
- spec/support/factory.rb
|
134
134
|
- spec/support/serializers.rb
|
135
|
+
has_rdoc:
|