Search code examples
jsonresthal-json

REST. Comparing two JSON representations


I have a tag system:

Tag

  • id
  • name
  • children tags

so they could be nested (max depth 3)

for example:

  • Food
    • Restaurant
    • Fast food
    • Chinese food
  • Medicine
    • Hospitals
    • Pharmacy
  • Entertainment
    • Extreme
      • Skiing
      • Skating
    • Family

And I have some resources that have tags.

Place

  • id
  • name
  • tags

And I have few endpoints:

/api/tags

{
  "items": [
    {
      "_links": {
        "self": {
          "href": "/api/tags/1"
        }
      },
      "id": 1,
      "name": "Food",
      "_embedded": {
        "children": [
          {
            "_links": {
              "self": {
                "href": "/api/tags/4"
              }
            },
            "id": 4,
            "name": "Restaurant",
            "_embedded": {
              "children": []
            }
          },
          {
            "_links": {
              "self": {
                "href": "/api/tags/5"
              }
            },
            "id": 5,
            "name": "Fast food",
            "_embedded": {
              "children": []
            }
          }
        ]
      }
    },
    {
      "_links": {
        "self": {
          "href": "/api/tags/2"
        }
      },
      "id": 2,
      "name": "Medicine",
      "_embedded": {
        "children": []
      }
    },
    {
      "_links": {
        "self": {
          "href": "/api/tags/3"
        }
      },
      "id": 3,
      "name": "Entertainment",
      "_embedded": {
        "children": []
      }
    }
  ]
}

And /api/place/1

{
  "id": 1,
  "name": "FooBar Arena",
  "_embedded": {
    "tags": [
      {
        "_links": {
          "self": {
            "href": "/api/tags/1"
          }
        },
        "id": 1,
        "name": "Food"
      },
      {
        "_links": {
          "self": {
            "href": "/api/tags/2"
          }
        },
        "id": 2,
        "name": "Medicine"
      }
    ]
  }
}

So I don't want tags to have embedded resources when they are listed as embedded resources themselves, but by NOT including embedded children I ended up with two different representations of the same data with the same SELF link, how clients should compare those? Comparing SELF links would work but one representation lacks children


Solution

  • I believe a good HAL client does not consider _embedded to be part of the representation. A good HAL client simply uses the values from _embedded to warm a cache. Items that appear in _embedded should still appear in _links.

    So as long as the tags appear in _links for every resource, there should be no need to embed the same resource more than once.