Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty one-to-one relationship incorrectly surfaced (should be surfaced as null) #277

Open
theirishpenguin opened this issue Dec 23, 2022 · 4 comments

Comments

@theirishpenguin
Copy link

Currently, if there is a one-to-one relationship and it is empty the related resources key gets removed from the "relationships" object. But, according to https://jsonapi.org/format/#document-resource-object-linkage this should not happen...

"Resource linkage MUST be represented as [...] null for empty to-one relationships."

Rather, the relationship data should be set to null - there is an example at https://stackoverflow.com/a/57414080

Here is an example...

{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      },
      ...

Expected Result

If author is null, we should get...

{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "data": null
      },
     ...

Actual Result

But if author is null, we actually get...

{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON:API paints my bikeshed!"
    },
    "relationships": {
   }
      ...

No author relationship.

@theirishpenguin
Copy link
Author

I've created a PR to workaround (maybe even solve or partially solve?) this issue at #278

I've annotated the code at https://github.com/beam-community/jsonapi/pull/278/files for a clearer explanation.

The key moment in the code is here where we can cleanly surface a nil without many code changes.

@theirishpenguin
Copy link
Author

Outstanding Work

If this approach bears fruit then we need to

  • Remove null relationship resources from the includes section as well
  • Add tests

@mattpolzin
Copy link
Member

mattpolzin commented Dec 23, 2022

My reading of the JSON:API specification has always left this as an implementation detail. In my reading, a relationships object (plural) must contain keys which are each valid relationship objects but it is not required to contain the same keys for every request for a certain resource. Therefore, a to-one relation that does not exist (may be null in some backend or database) can either be omitted entirely from the relationships object or else it must be serialized properly as a relationship object which is to say it is {"data": null}.

In my opinion, it is nonetheless unfortunate for a library to pick one or the other because I actually feel there's a useful distinction between omitting and nulling out in an API contract and both should be possible depending on the API.

At any rate, I would call this a feature request more than a bug based on my understanding of the specification.

@theirishpenguin
Copy link
Author

Hi @mattpolzin, thanks for the quick reply. I haven't internalised the JSON:API specification in detail in this area - your reading of it sounds reasonable to me and I am happy to yield to that interpretation :-) I agree that supporting both would be ideal if that is what the spec allows. So I'd be in agreement with your above suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants