Skip to content

Official extension proposal: Embedding related resources #536

@dgeb

Description

@dgeb

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. The embedded member could be included alongside the linkage 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    extensionRelated to existing and proposed extensions as well as extensions in general

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy