JsonApiClient / json_api_client

Build client libraries compliant with specification defined by jsonapi.org
MIT License
362 stars 186 forks source link

Fallback to fetching the association if relationship data was not found in included #339

Closed kamal closed 4 years ago

kamal commented 5 years ago

Given a payload that looks something like

{
  "data": {
    ...
    "relationships": {
      "owner": {
        "links": { ... },
        "data": { "type": "owners", "id": 1 }
      }
    }
  }
}

where the relationship has data, but whose full data does not appear in included, the current code will raise an exception undefined method '[]' for nil:NilClass.

This PR changes how the included data is accessed that avoids this exception, by using Hash#dig. It then falls back to fetching the relationship via a request.

senid231 commented 5 years ago

this is looks like a bug on server side if relationship has data corresponding object must be included in payload @kamal do you have real life case with such behaviour?

abuiles commented 5 years ago

@senid231 by reading the spec and based on interactions with other clients (ember-data) --- it doesn't seem like you must include a resource in the payload if you are using a resource linkage in the relationships data.

From https://jsonapi.org/format/#document-compound-documents

Resource linkage in a compound document allows a client to link together all of the included resource objects without having to GET any URLs via links.

The behavior I've seen is: if there is a linkage and the resources in included then user it, otherwise GET it.

However, if you include a resource in included, then it must have a linkage:

In a compound document, all included resources MUST be represented as an array of resource objects in a top-level included member -

senid231 commented 5 years ago

@abuiles I get your point. Indeed I can't find in spec requirement that object must be present in included or dataset if it's id and type are present in relationships. I've always used it as de facto rule