You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Separate related resources in a top-level linked object.
It's important to separate the primary resource from related resources because:
* It allows the client to easily identify the primary resource among related resources of the same type. This is especially important when creating a resource without prior knowledge of its server-assigned id.
* It clarifies issues like pagination that are specific to the primary resource.
This can be achieved by nesting related resources in a `linked` object. For instance:
```javascript
{
"posts": [{
"id": "1",
"name": "Rails is Omakase",
"links": {
"author": "9",
"related": ["2"]
}
}],
"linked": {
"posts": [{
"id": "2",
"name": "The Parley Letter",
"links": {
"author": "9",
"related": ["1"]
}
}],
"people": [{
"id": "9",
"name": "DHH"
}]
}
}
```
It's easy to imagine a scenario in which resources that are members of the primary array are secondary resources for other members of the primary array (e.g. people + friends). In those scenarios, primary resources should not be duplicated as secondary resources in `linked`. In other words, a single canonical representation of a resource should continue to be returned with each response.
[Closes#74]
Copy file name to clipboardExpand all lines: format/index.md
+40-28Lines changed: 40 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,8 @@ attributes section and should not be used as attribute names.
24
24
The top-level of a JSON API document **MAY** have the following keys:
25
25
26
26
*`meta`: meta-information about a resource, such as pagination
27
-
* Other resource names (`posts`, `comments`, `people`, etc.)
27
+
*`linked`: a collection of documents, grouped by type, that are related to the
28
+
primary document and/or each other
28
29
29
30
### Reserved Attributes
30
31
@@ -197,7 +198,8 @@ The top-level of a JSON API document **MAY** have the following keys:
197
198
198
199
*`meta`: meta-information about a resource, such as pagination
199
200
*`links`: URL templates to be used for expanding resources' relationships URLs
200
-
* Other resource names (`posts`, `comments`, `people`, etc.)
201
+
*`linked`: a collection of documents, grouped by type, that are related to the
202
+
primary document and/or each other
201
203
202
204
### Singular Resources
203
205
@@ -397,7 +399,10 @@ Top-level URL templates allow you to specify relationships as IDs, but without r
397
399
398
400
To save HTTP requests, it may be convenient to send related documents along with the requested documents.
399
401
400
-
In this case, a bit of extra metadata for each relationship can link together the documents.
402
+
Related documents **MUST** be included in a top level `"linked"` object, in which they are grouped together in arrays according to their type.
403
+
404
+
The type of each relationship **MAY** be specified in the `"links"` object with
405
+
the `"type"` key. This facilitates lookups of related documents by the client.
401
406
402
407
```javascript
403
408
{
@@ -431,35 +436,42 @@ In this case, a bit of extra metadata for each relationship can link together th
431
436
"comments": [ "6" ]
432
437
}
433
438
}],
434
-
"author": [{
435
-
"id":"9",
436
-
"name":"@d2h"
437
-
}],
438
-
"comments": [{
439
-
"id":"1",
440
-
"body":"Mmmmmakase"
441
-
}, {
442
-
"id":"2",
443
-
"body":"I prefer unagi"
444
-
}, {
445
-
"id":"3",
446
-
"body":"What's Omakase?"
447
-
}, {
448
-
"id":"4",
449
-
"body":"Parley is a discussion, especially one between enemies"
450
-
}, {
451
-
"id":"5",
452
-
"body":"The parsley letter"
453
-
}, {
454
-
"id":"6",
455
-
"body":"Dependency Injection is Not a Vice"
456
-
}]
439
+
"linked": {
440
+
"people": [{
441
+
"id":"9",
442
+
"name":"@d2h"
443
+
}],
444
+
"comments": [{
445
+
"id":"1",
446
+
"body":"Mmmmmakase"
447
+
}, {
448
+
"id":"2",
449
+
"body":"I prefer unagi"
450
+
}, {
451
+
"id":"3",
452
+
"body":"What's Omakase?"
453
+
}, {
454
+
"id":"4",
455
+
"body":"Parley is a discussion, especially one between enemies"
456
+
}, {
457
+
"id":"5",
458
+
"body":"The parsley letter"
459
+
}, {
460
+
"id":"6",
461
+
"body":"Dependency Injection is Not a Vice"
462
+
}]
463
+
}
457
464
}
458
465
```
459
466
460
-
The benefit of this approach is that when the same document is referenced multiple times (in this example, the author of the three posts), it only needs to be presented a single time in the document.
467
+
This approach ensures that a single canonical representation of each document
468
+
is returned with each response, even when the same document is referenced
469
+
multiple times (in this example, the author of the three posts). Along these
470
+
lines, if a primary document is linked to another primary or related document,
471
+
it should not be duplicated within the `"linked"` object.
461
472
462
-
By always combining documents in this way, a client can consistently extract and wire up references.
473
+
By always combining documents in this way, a client can consistently extract and
474
+
wire up references.
463
475
464
476
JSON API documents **MAY** specify the URL for a document in a compound
0 commit comments