You Care
You Care

Reputation: 488

REST. Comparing two JSON representations

I have a tag system:

Tag

so they could be nested (max depth 3)

for example:

And I have some resources that have tags.

Place

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

Upvotes: 1

Views: 77

Answers (1)

Evert
Evert

Reputation: 99541

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.

Upvotes: 1

Related Questions