danivek/json-api-serializer

Error when try to deserialise an included relationship with `alternativeKey`

max-konin opened this issue · 3 comments

Test example:

const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
  relationships: {
    author: {
      type: 'people',
      alternativeKey: 'author_id',
      deserialize: data => data,
    }
  }
});

Serializer.register('people', {});

const data = {
  data: {
    type: 'article',
    id: '1',
    attributes: {
      title: 'JSON API paints my bikeshed!',
      body: 'The shortest article. Ever.',
      created: '2015-05-22T14:56:29.000Z'
    },
    relationships: {
      author: {
        data: {
          type: 'people',
          id: '1'
        }
      }
    }
  },
  included: [
    {
      type: 'people',
      id: '1',
      attributes: {
        name: 'noname'
      }
    }
  ]
};

const deserializedData = Serializer.deserialize('articles', data);
expect(deserializedData.author).deep.equal({
  id: '1',
  name: 'noname'
})
done();

Actual behavior:

TypeError: Cannot read property 'schema' of undefined
      at JSONAPISerializer.deserializeIncluded (lib/JSONAPISerializer.js:116:1534)
      at Object.keys.forEach.relationshipProperty (lib/JSONAPISerializer.js:116:178)
      at Array.forEach (<anonymous>)
      at JSONAPISerializer.deserializeResource (lib/JSONAPISerializer.js:114:130)
      at JSONAPISerializer.deserialize (lib/JSONAPISerializer.js:69:1152)
      at Context.<anonymous> (test/unit/JSONAPISerializer.test.js:1713:43)

I guess the deserializer should not use alternativeKey. I can fix it. But my guess may be wrong

You need to registrer 2 types: article and people. Here you have only articles

@danivek I've registered people type, ofc.

Ok, I see what happen, I will fix it