-
Notifications
You must be signed in to change notification settings - Fork 890
Description
The spec has been dancing around the concept of embedding related resources in create / update requests for some time (ref #131, #165, #202).
I don't favor "side-posting" (see #202) as a general solution because:
- it potentially merges "new" and "pre-existing" objects in a single representation of linkage
- it relies on client-generated IDs to provide linkages
Instead, the alternative that I've favored is the JSON Patch extension. The order of operations explicitly requires related resources to be created before parent resources, so there's no ambiguity.
However, there are some problems with proposing the JSON Patch extension as a general solution:
- it is not additive to the base spec, and therefore requires a lot more work to implement
- it also relies on client-generated IDs to provide linkages
So, I'd like to propose an extension that has none of the limitations of side-posting or JSON Patch: an official "embedded" extension. Here's how I envision it:
A link object could contain an
embedded
member that would include representations of new resources. Theembedded
member could be included alongside thelinkage
member, so that both pre-existing and new resources could be referenced.
I believe this would be very useful for creating resources along with a POST or PATCH operation to a parent resource.
Here's an example that links a new article to two pre-existing tags and creates two new ones:
POST /articles
Content-Type: application/vnd.api+json; ext=embedded
Accept: application/vnd.api+json; ext=embedded
{
"data": {
"type": "articles",
"title": "Flask is à la carte",
"links": {
"tags": {
"linkage": [
{"type": "tags", "id": "1"},
{"type": "tags", "id": "2"}
],
"embedded": [
{"type": "tags", "label": "JSON"},
{"type": "tags", "label": "REST"}
]
}
}
}
}
Here's an example that replaces an existing article's tags with two new ones:
PATCH /articles/1
Content-Type: application/vnd.api+json; ext=embedded
Accept: application/vnd.api+json; ext=embedded
{
"data": {
"type": "articles",
"id": "1",
"links": {
"tags": {
"embedded": [
{"type": "tags", "label": "JSON"},
{"type": "tags", "label": "REST"}
]
}
}
}
}
A few considerations:
- As an additive extension, this is not a blocker to 1.0.
- I don't believe that we need to support embedding at relationship URLs, which only accept linkage data.
- This representation provides no benefits for fetching related resources, so I don't want to encourage its use (although I'm not sure we can/should prohibit it either).
Edit 4/23 - Please see #536 (comment) for the latest version of this proposal.