jsonapi-serializers 0.1.2 → 0.2.0
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 +31 -18
- data/lib/jsonapi-serializers/serializer.rb +12 -8
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +56 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0218413bec5edb7e7793360856d93a507c4ff6a3
|
4
|
+
data.tar.gz: 6a363b7e853a348168aa9bb8d6f6018254d88ee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af9bb1074fe08168f70b1c5ecb1907aa34683fea230609e3334d974a4c45e9add97fb8492271cb3b488eb11ae2a5ca71528456f1437289a5302905de767c6bcf
|
7
|
+
data.tar.gz: 8eebd11b6ae280466788c87836156dcfe15b078b4eb27a24b3f3568cc90d0e8d460226045edaf5958bcee9f025ee01274fd926a6bad1650de609d21d9bf3ae62
|
data/README.md
CHANGED
@@ -3,10 +3,9 @@
|
|
3
3
|
[](https://travis-ci.org/fotinakis/jsonapi-serializers)
|
4
4
|
[](http://badge.fury.io/rb/jsonapi-serializers)
|
5
5
|
|
6
|
-
|
7
6
|
JSONAPI::Serializers is a simple library for serializing Ruby objects and their relationships into the [JSON:API format](http://jsonapi.org/format/).
|
8
7
|
|
9
|
-
|
8
|
+
This library is up-to-date with the finalized v1 JSON API spec.
|
10
9
|
|
11
10
|
* [Features](#features)
|
12
11
|
* [Installation](#installation)
|
@@ -53,7 +52,7 @@ require 'jsonapi-serializers'
|
|
53
52
|
|
54
53
|
class PostSerializer
|
55
54
|
include JSONAPI::Serializer
|
56
|
-
|
55
|
+
|
57
56
|
attribute :title
|
58
57
|
attribute :content
|
59
58
|
end
|
@@ -280,17 +279,21 @@ Returns:
|
|
280
279
|
},
|
281
280
|
"relationships": {
|
282
281
|
"author": {
|
283
|
-
"
|
284
|
-
|
285
|
-
|
282
|
+
"links": {
|
283
|
+
"self": "/posts/1/links/author",
|
284
|
+
"related": "/posts/1/author"
|
285
|
+
},
|
286
|
+
"data": {
|
286
287
|
"type": "users",
|
287
288
|
"id": "1"
|
288
289
|
}
|
289
290
|
},
|
290
291
|
"comments": {
|
291
|
-
"
|
292
|
-
|
293
|
-
|
292
|
+
"links": {
|
293
|
+
"self": "/posts/1/links/comments",
|
294
|
+
"related": "/posts/1/comments"
|
295
|
+
},
|
296
|
+
"data": [
|
294
297
|
{
|
295
298
|
"type": "comments",
|
296
299
|
"id": "1"
|
@@ -322,16 +325,20 @@ Returns:
|
|
322
325
|
},
|
323
326
|
"relationships": {
|
324
327
|
"user": {
|
325
|
-
"
|
326
|
-
|
327
|
-
|
328
|
+
"links": {
|
329
|
+
"self": "/comments/1/links/user",
|
330
|
+
"related": "/comments/1/user"
|
331
|
+
},
|
332
|
+
"data": {
|
328
333
|
"type": "users",
|
329
334
|
"id": "2"
|
330
335
|
}
|
331
336
|
},
|
332
337
|
"post": {
|
333
|
-
"
|
334
|
-
|
338
|
+
"links": {
|
339
|
+
"self": "/comments/1/links/post",
|
340
|
+
"related": "/comments/1/post"
|
341
|
+
}
|
335
342
|
}
|
336
343
|
}
|
337
344
|
},
|
@@ -353,13 +360,13 @@ Returns:
|
|
353
360
|
Notice a few things:
|
354
361
|
* The [primary data](http://jsonapi.org/format/#document-structure-top-level) relationships now include "linkage" information for each relationship that was included.
|
355
362
|
* The related objects themselves are loaded in the top-level `included` member.
|
356
|
-
* The related objects _also_ include "linkage"
|
357
|
-
|
363
|
+
* The related objects _also_ include "linkage" data 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 data for whatever `include` paths you specify. This conforms to this part of the spec:
|
364
|
+
|
358
365
|
> 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-
|
366
|
+
> [JSON:API Compound Documents](http://jsonapi.org/format/#document-compound-documents)
|
360
367
|
|
361
368
|
#### Relationship path handling
|
362
|
-
|
369
|
+
|
363
370
|
The `include` param also accepts a string of [relationship paths](http://jsonapi.org/format/#fetching-includes), ie. `include: 'author,comments,comments.user'` so you can pass an `?include` query param directly through to the serialize method. Be aware that letting users pass arbitrary relationship paths might introduce security issues depending on your authorization setup, where a user could `include` a relationship they might not be authorized to see directly. Be aware of what you allow API users to include.
|
364
371
|
|
365
372
|
## Rails example
|
@@ -425,6 +432,10 @@ end
|
|
425
432
|
* Support for the `fields` spec is planned, would love a PR contribution for this.
|
426
433
|
* Support for pagination/sorting is unlikely to be supported because it would likely involve coupling to ActiveRecord, but please open an issue if you have ideas of how to support this generically.
|
427
434
|
|
435
|
+
## Release notes
|
436
|
+
|
437
|
+
* v0.2.0: Initial release with support for the final v1 JSON API spec.
|
438
|
+
|
428
439
|
## Contributing
|
429
440
|
|
430
441
|
1. Fork it ( https://github.com/fotinakis/jsonapi-serializers/fork )
|
@@ -432,3 +443,5 @@ end
|
|
432
443
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
433
444
|
4. Push to the branch (`git push origin my-new-feature`)
|
434
445
|
5. Create a new Pull Request
|
446
|
+
|
447
|
+
Throw a ★ on it! :)
|
@@ -87,19 +87,21 @@ module JSONAPI
|
|
87
87
|
has_one_relationships.each do |attribute_name, object|
|
88
88
|
formatted_attribute_name = format_name(attribute_name)
|
89
89
|
data[formatted_attribute_name] = {
|
90
|
-
'
|
91
|
-
|
90
|
+
'links' => {
|
91
|
+
'self' => relationship_self_link(attribute_name),
|
92
|
+
'related' => relationship_related_link(attribute_name),
|
93
|
+
},
|
92
94
|
}
|
93
95
|
if @_include_linkages.include?(formatted_attribute_name)
|
94
96
|
if object.nil?
|
95
97
|
# Spec: Resource linkage MUST be represented as one of the following:
|
96
98
|
# - null for empty to-one relationships.
|
97
99
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
98
|
-
data[formatted_attribute_name].merge!({'
|
100
|
+
data[formatted_attribute_name].merge!({'data' => nil})
|
99
101
|
else
|
100
102
|
related_object_serializer = JSONAPI::Serializer.find_serializer(object)
|
101
103
|
data[formatted_attribute_name].merge!({
|
102
|
-
'
|
104
|
+
'data' => {
|
103
105
|
'type' => related_object_serializer.type.to_s,
|
104
106
|
'id' => related_object_serializer.id.to_s,
|
105
107
|
},
|
@@ -112,19 +114,21 @@ module JSONAPI
|
|
112
114
|
has_many_relationships.each do |attribute_name, objects|
|
113
115
|
formatted_attribute_name = format_name(attribute_name)
|
114
116
|
data[formatted_attribute_name] = {
|
115
|
-
'
|
116
|
-
|
117
|
+
'links' => {
|
118
|
+
'self' => relationship_self_link(attribute_name),
|
119
|
+
'related' => relationship_related_link(attribute_name),
|
120
|
+
},
|
117
121
|
}
|
118
122
|
# Spec: Resource linkage MUST be represented as one of the following:
|
119
123
|
# - an empty array ([]) for empty to-many relationships.
|
120
124
|
# - an array of linkage objects for non-empty to-many relationships.
|
121
125
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
122
126
|
if @_include_linkages.include?(formatted_attribute_name)
|
123
|
-
data[formatted_attribute_name].merge!({'
|
127
|
+
data[formatted_attribute_name].merge!({'data' => []})
|
124
128
|
objects = objects || []
|
125
129
|
objects.each do |obj|
|
126
130
|
related_object_serializer = JSONAPI::Serializer.find_serializer(obj)
|
127
|
-
data[formatted_attribute_name]['
|
131
|
+
data[formatted_attribute_name]['data'] << {
|
128
132
|
'type' => related_object_serializer.type.to_s,
|
129
133
|
'id' => related_object_serializer.id.to_s,
|
130
134
|
}
|
data/spec/serializer_spec.rb
CHANGED
@@ -42,12 +42,16 @@ describe JSONAPI::Serializer do
|
|
42
42
|
},
|
43
43
|
'relationships' => {
|
44
44
|
'user' => {
|
45
|
-
'
|
46
|
-
|
45
|
+
'links' => {
|
46
|
+
'self' => '/long-comments/1/links/user',
|
47
|
+
'related' => '/long-comments/1/user',
|
48
|
+
},
|
47
49
|
},
|
48
50
|
'post' => {
|
49
|
-
'
|
50
|
-
|
51
|
+
'links' => {
|
52
|
+
'self' => '/long-comments/1/links/post',
|
53
|
+
'related' => '/long-comments/1/post',
|
54
|
+
},
|
51
55
|
},
|
52
56
|
},
|
53
57
|
})
|
@@ -91,12 +95,16 @@ describe JSONAPI::Serializer do
|
|
91
95
|
'relationships' => {
|
92
96
|
# Both to-one and to-many links are present, but neither include linkage:
|
93
97
|
'author' => {
|
94
|
-
'
|
95
|
-
|
98
|
+
'links' => {
|
99
|
+
'self' => '/posts/1/links/author',
|
100
|
+
'related' => '/posts/1/author',
|
101
|
+
},
|
96
102
|
},
|
97
103
|
'long-comments' => {
|
98
|
-
'
|
99
|
-
|
104
|
+
'links' => {
|
105
|
+
'self' => '/posts/1/links/long-comments',
|
106
|
+
'related' => '/posts/1/long-comments',
|
107
|
+
},
|
100
108
|
},
|
101
109
|
},
|
102
110
|
})
|
@@ -122,17 +130,21 @@ describe JSONAPI::Serializer do
|
|
122
130
|
},
|
123
131
|
'relationships' => {
|
124
132
|
'author' => {
|
125
|
-
'
|
126
|
-
|
133
|
+
'links' => {
|
134
|
+
'self' => '/posts/1/links/author',
|
135
|
+
'related' => '/posts/1/author',
|
136
|
+
},
|
127
137
|
# Spec: Resource linkage MUST be represented as one of the following:
|
128
138
|
# - null for empty to-one relationships.
|
129
139
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
130
|
-
'
|
140
|
+
'data' => nil,
|
131
141
|
},
|
132
142
|
'long-comments' => {
|
133
|
-
'
|
134
|
-
|
135
|
-
|
143
|
+
'links' => {
|
144
|
+
'self' => '/posts/1/links/long-comments',
|
145
|
+
'related' => '/posts/1/long-comments',
|
146
|
+
},
|
147
|
+
'data' => [],
|
136
148
|
},
|
137
149
|
},
|
138
150
|
})
|
@@ -156,20 +168,24 @@ describe JSONAPI::Serializer do
|
|
156
168
|
},
|
157
169
|
'relationships' => {
|
158
170
|
'author' => {
|
159
|
-
'
|
160
|
-
|
171
|
+
'links' => {
|
172
|
+
'self' => '/posts/1/links/author',
|
173
|
+
'related' => '/posts/1/author',
|
174
|
+
},
|
161
175
|
# Spec: Resource linkage MUST be represented as one of the following:
|
162
176
|
# - a 'linkage object' (defined below) for non-empty to-one relationships.
|
163
177
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
164
|
-
'
|
178
|
+
'data' => {
|
165
179
|
'type' => 'users',
|
166
180
|
'id' => '1',
|
167
181
|
},
|
168
182
|
},
|
169
183
|
'long-comments' => {
|
170
|
-
'
|
171
|
-
|
172
|
-
|
184
|
+
'links' => {
|
185
|
+
'self' => '/posts/1/links/long-comments',
|
186
|
+
'related' => '/posts/1/long-comments',
|
187
|
+
},
|
188
|
+
'data' => [],
|
173
189
|
},
|
174
190
|
},
|
175
191
|
})
|
@@ -193,17 +209,21 @@ describe JSONAPI::Serializer do
|
|
193
209
|
},
|
194
210
|
'relationships' => {
|
195
211
|
'author' => {
|
196
|
-
'
|
197
|
-
|
198
|
-
|
212
|
+
'links' => {
|
213
|
+
'self' => '/posts/1/links/author',
|
214
|
+
'related' => '/posts/1/author',
|
215
|
+
},
|
216
|
+
'data' => nil,
|
199
217
|
},
|
200
218
|
'long-comments' => {
|
201
|
-
'
|
202
|
-
|
219
|
+
'links' => {
|
220
|
+
'self' => '/posts/1/links/long-comments',
|
221
|
+
'related' => '/posts/1/long-comments',
|
222
|
+
},
|
203
223
|
# Spec: Resource linkage MUST be represented as one of the following:
|
204
224
|
# - an empty array ([]) for empty to-many relationships.
|
205
225
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
206
|
-
'
|
226
|
+
'data' => [],
|
207
227
|
},
|
208
228
|
},
|
209
229
|
})
|
@@ -228,17 +248,21 @@ describe JSONAPI::Serializer do
|
|
228
248
|
},
|
229
249
|
'relationships' => {
|
230
250
|
'author' => {
|
231
|
-
'
|
232
|
-
|
233
|
-
|
251
|
+
'links' => {
|
252
|
+
'self' => '/posts/1/links/author',
|
253
|
+
'related' => '/posts/1/author',
|
254
|
+
},
|
255
|
+
'data' => nil,
|
234
256
|
},
|
235
257
|
'long-comments' => {
|
236
|
-
'
|
237
|
-
|
258
|
+
'links' => {
|
259
|
+
'self' => '/posts/1/links/long-comments',
|
260
|
+
'related' => '/posts/1/long-comments',
|
261
|
+
},
|
238
262
|
# Spec: Resource linkage MUST be represented as one of the following:
|
239
263
|
# - an array of linkage objects for non-empty to-many relationships.
|
240
264
|
# http://jsonapi.org/format/#document-structure-resource-relationships
|
241
|
-
'
|
265
|
+
'data' => [
|
242
266
|
{
|
243
267
|
'type' => 'long-comments',
|
244
268
|
'id' => '1',
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2015-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|