Question

Suppose Person has a Car. Car is a separate resource with its own URI. For the sake of this example, assume a person can only have one car.

We want to include the Person's Car in the response when requesting a person via /person/{id}, but we don't want to include the entire Car resource with all of its properties which may be a complicated nested structure. We only want to include a few of the main properties, such as Color, Make, and Model, along with a link to the full Car resource.

  1. Is it normal to have a different representation of Car that only exists as a child property of Person? Car may also have even more simplified representations as a child of other resources as well, depending on what properties it makes sense to expose based on the parent resource.

  2. Since I'm not including an "official" representation of the Car resource (one that has its own URI) inside Person, do I include the link to the full Car resource inside the child Car property on Person or do I put the link in the list of links on the parent resource Person?

Include link on the parent resource Person:

{
  "Person": {
    "Name": "John Smith",
    "Age": 25,
    "Car": {
      "Make": "Toyota",
      "Model": "Camry",
      "Year": 2012
    },
    "Links": [
      {
        "rel": "car",
        "href": "/person/{id}/car"
      },
      {
        "rel": "some-other-resource",
        "href": "/person/{id}/some-other-resource"
      }
    ]
  }
}

Or include the Car link in the child property Car:

{
  "Person": {
    "Name": "John Smith",
    "Age": 25,
    "Car": {
      "Make": "Toyota",
      "Model": "Camry",
      "Year": 2012,
      "Links": [
        {
          "rel": "self",
          "href": "/person/{id}/car"
        }
      ]
    },
    "Links": [
      {
        "rel": "some-other-resource",
        "href": "/person/{id}/some-other-resource"
      }
    ]
  }
}
Was it helpful?

Solution

Is it normal to have a different representation of Car that only exists as a child property of Person?

The world wide web is littered with examples of this pattern; index pages with summaries and links to more detailed documents, descriptions of images linked to the images themselves, summaries of shopping carts with links to a more detailed interface....

do I include the link to the full Car resource inside the child Car property on Person or do I put the link in the list of links on the parent resource Person?

Either of these can be fine. The representation with the link inside the car may be more useful when you have cases that need a collection of cars, each with a link of their own.

Licensed under: CC-BY-SA with attribution
scroll top