0

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"
      }
    ]
  }
}
  • do I include the link to the full Car resource -- If you want the consuming application to have the ability to navigate to the full Car resource. – Robert Harvey Apr 23 at 0:32
  • Is it normal to have a different representation of Car that only exists as a child property of Person? -- I would imagine that the way you represent Person in this context is entirely up to you. – Robert Harvey Apr 23 at 0:33
  • @RobertHarvey "do I include the link to the full Car resource" was not my full question – wired_in Apr 23 at 15:38
  • My answer is still the same. Include the link to the full car resource if you want the consuming application to have the ability to navigate there. – Robert Harvey Apr 23 at 15:43
  • @RobertHarvey, That doesn't answer my question. My question was not whether the link to the full resource should be included or not. My question was in regards to where the link should be placed inside the Person resource. There seems to be a reading comprehension issue here. – wired_in Apr 23 at 17:03
2

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.

| improve this answer | |
  • Word, thanks brah – wired_in Apr 23 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.