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

Errors when response has empty relationships #3

Open
andrepcg opened this issue Sep 21, 2019 · 3 comments · May be fixed by #4
Open

Errors when response has empty relationships #3

andrepcg opened this issue Sep 21, 2019 · 3 comments · May be fixed by #4

Comments

@andrepcg
Copy link

andrepcg commented Sep 21, 2019

In my backend I have a conversation model that olds a foreign_key to the last sent message in that conversation. If there is no last message, this normaliser fails.

Example:

import normalize from 'normalize-json-api-response';

const json = {
   "data":[
      {
         "id":"3",
         "type":"conversation",
         "attributes":{
            "created_at":"2019-09-19T10:03:44.401Z",
            "subject":"Test subject"
         },
         "relationships":{
            "last_message":{
               "data":null
            }
         }
      },
   ],
   "included":[]
};

normalize(json)

Uncaught (in promise) TypeError: Cannot read property 'constructor' of null at normalize (index.js:57) (

if (relation.data.constructor === Array) {
)

I also had occasions where it would fail on line 53 (

if (!relation.data[0].hasOwnProperty('type')) {
) when relation.data[0] would be undefined.

I have temporarily fixed this in the backend by not serializing fields that are empty but that's going to be cumbersome to do across all serializers.


Also, if the serializer does not include any relationship, and thus the included attribute does not exist, the normalizer crashes as well (

response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
). Do you plan on supporting these "features"? I can fork the repo use that if it's something you don't want to support

@SinestroWhite
Copy link
Owner

SinestroWhite commented Oct 20, 2019

The problem does not come from the empty included field. Look at your last_message in your example:
"relationships":{ "last_message":{ "data":null } }
the data field here must not be null. It has to be something of this sort
{ "type": "people", "id": "9" }

The reason it crashes on line 53 is because the data field does not have type subfield.
This is required by the JSON::API specification. If you don't have a last_massage relation, modify your API so that it doesn't respond with a null value.

Thank you very much for your contribution. I will review it and I will post a comment here at a later date.

@andrepcg
Copy link
Author

I'm using https://github.com/Netflix/fast_jsonapi and it's not directly compatible with this lib without making some backend changes. Basically, if a model has a relationship and it's empty (by empty I mean it's null), fast_jsonapi serializes it to null.

@SinestroWhite
Copy link
Owner

Yeah, I got it and you are right that a null value must not give an error.

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

Successfully merging a pull request may close this issue.

2 participants