yin icon indicating copy to clipboard operation
yin copied to clipboard

Hydrating same type (child, parent) related entity

Open Vivenco opened this issue 5 years ago • 2 comments

Hi all,

I need help with this one.

When I have json:api response data with relationship of same type (e.g. parent or child - relationship with it-self) and their id's are the same, Hydrator is not capable creating those relationships.

Response example:

Page with id:15 has parent with id:14, but its not present in included because it already exists in data array.

  "data": [
    {
      "type": "page",
      "id": "15",
      "attributes": {
        "internal_name": "Company",
        "active": 1,
        "parent_id": 14
      },
      "relationships": {
        "parent": {
          "data": {
            "type": "page",
            "id": "14"
          }
        }
      }
    },
    {
      "type": "page",
      "id": "14",
      "attributes": {
        "internal_name": "About us",
        "parent_id": null
      },
      "relationships": {
        "parent": {
          "data": null
        }
      }
    },
    {
      "type": "page",
      "id": "13",
      "attributes": {
        "internal_name": "Information",
        "parent_id": 1
      },
      "relationships": {
        "parent": {
          "data": {
            "type": "page",
            "id": "1"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "page",
      "id": "1",
      "attributes": {
        "internal_name": "General",
        "parent_id": null
      }
    }
  ]
}

Vivenco avatar Jun 12 '19 10:06 Vivenco

Hi @Vivenco,

This is the intended behaviour. The authors of the JSON:API standard wrote somewhere that the response is basically an identity map where none of the items (regardless if they are in the primary data or included) doesn't appear more than once.

Does this answer solve your problem?

kocsismate avatar Jun 20 '19 13:06 kocsismate

@Vivenco I've just realized that you are talking about the Hydrator (and not about transformers)! Sorry for confusion, I'll have a look at the issue as soon as I have some spare time.

kocsismate avatar Jun 24 '19 12:06 kocsismate