reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Empty relationship data when updating resource with optional relationship. #132

Closed Maan511 closed 5 years ago

Maan511 commented 5 years ago

When saving a resource with an optional to-one relationship which has not been set, a relationship object with an empty data attribute is sent to the server, along with other updates.

{
  "data": {
    // "id", "type" and "attributes" left out
    "relationships": {
      "Part": { "data": { } }
    }
  }
}

If I understand the documentation correctly, this is not valid.

The PATCH request MUST include a top-level member named data containing one of:

  • a resource identifier object corresponding to the new related resource.
  • null, to remove the relationship.

In my case where the relationship is not set to a resource, I would expect the Part relationship object to be left out of the update request,

pablorsk commented 5 years ago

Can you build a small example? you are using new() or edition a received resource?

Maan511 commented 5 years ago

Hi @pablorsk

It is a received resource.

The problem occurs when I:

  1. Fetch resources from server that have a to-one relationship, which has not been set.
  2. Call .save() on one of the resources. Then "relationship-name": { "data": { } } is sent to the server.

I believe it originates from here: https://github.com/reyesoft/ngx-jsonapi/blob/dd6b0a721cba0f1231152c9528239e48eac6e3c6/src/resource.ts#L87

I will try to build an example, as soon as time allows it.

maxi7587 commented 5 years ago

I think we can close this issue, since #141 fixed empty relationships structure...