Skip to content

Errors when response has empty relationships #3

Open

Description

@andrepcg

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

Activity

  1. SinestroWhite commented on Oct 20, 2019

    @SinestroWhite
    Owner

    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.

  2. andrepcg commented on Oct 20, 2019

    @andrepcg
    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.

  3. SinestroWhite commented on Oct 20, 2019

    @SinestroWhite
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions