jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

Clearing a resource relationship

Open bensullivan opened this issue 6 years ago • 3 comments

Hi

We have a requirement to be able to clear (make null) an existing relationship on a resource. As far as I'm aware the JSON:API spec supports this (where prior to this example PATCH request, the author relationship was "data": { "type": "people", "id": "1" }):

PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "articles",
    "id": "1",
    "relationships": {
      "author": {
        "data": null
      }
    }
  }
}

When I come to deserialise a request like this I am getting a InvalidJsonApiResourceException: Resource must contain at least one of 'data', 'error' or 'meta' nodes.

Assuming my interpretation of the JSON:API spec for this scenario isn't wrong, is there a way to clear resource relationships with jsonapi-converter?

Thanks for your help!

Ben

bensullivan avatar Oct 30 '19 03:10 bensullivan

Hi @jasminb - appreciate you're likely very busy at the moment; I would really appreciate any historical/contextual insight into the above before I begin debugging the code to figure out a path forward for us. Many thanks for your efforts on this library - it's very valuable. Ben

bensullivan avatar Nov 06 '19 09:11 bensullivan

Hello @bensullivan, internally same logic that is used to parse primary data is used to parse relationship data and thats why the same rules apply. This should be something that can be fixed/made configurable.

Edit: Please disregard this comment, I did not read your question properly.

jasminb avatar Nov 06 '19 18:11 jasminb

Just tested parsing following payload (on both master and develop):

{
  "data": {
    "type": "engineer",
    "id": "id",
    "relationships": {
      "field": {
        "data": null
      },
      "city": {
        "data": null
      }
    }
  }
}

And it correctly produces the desired type with its id set and other values set to null including relationship objects.

jasminb avatar Nov 06 '19 18:11 jasminb