diff --git a/lib/JSONAPISerializer.js b/lib/JSONAPISerializer.js index 869c45f..67f2a62 100644 --- a/lib/JSONAPISerializer.js +++ b/lib/JSONAPISerializer.js @@ -435,7 +435,7 @@ module.exports = class JSONAPISerializer { ? this.deserializeIncluded( d.type, d.id, - options.relationships[relationshipProperty], + options.relationships[relationshipKey], included ) : deserializeFunction(d) @@ -452,7 +452,7 @@ module.exports = class JSONAPISerializer { ? this.deserializeIncluded( relationship.data.type, relationship.data.id, - options.relationships[relationshipProperty], + options.relationships[relationshipKey], included ) : deserializeFunction(relationship.data) diff --git a/test/unit/JSONAPISerializer.test.js b/test/unit/JSONAPISerializer.test.js index 19ac2e6..adfb783 100644 --- a/test/unit/JSONAPISerializer.test.js +++ b/test/unit/JSONAPISerializer.test.js @@ -1533,7 +1533,15 @@ describe('JSONAPISerializer', function() { it('should deserialize with \'unconvertCase\' options', function(done) { const Serializer = new JSONAPISerializer(); - Serializer.register('articles', { + Serializer.register('article', { + unconvertCase: 'snake_case', + relationships: { + article_author: { + type: 'people', + }, + } + }); + Serializer.register('people', { unconvertCase: 'snake_case', }); @@ -1552,23 +1560,28 @@ describe('JSONAPISerializer', function() { } } } - } + }, + included: [{ + type: 'people', + id: '1', + attributes: { firstName: 'Karl' } + }] }; - const deserializedData = Serializer.deserialize('articles', data); + const deserializedData = Serializer.deserialize('article', data); expect(deserializedData).to.have.property('created_at'); - expect(deserializedData).to.have.property('article_author'); + expect(deserializedData.article_author).to.deep.equal({ id: '1', first_name: 'Karl' }); done(); }); it('should deserialize with \'unconvertCase\' and \'deserialize\' options', function(done) { const Serializer = new JSONAPISerializer(); - Serializer.register('articles', { + Serializer.register('article', { unconvertCase: 'snake_case', relationships: { article_author: { - deserialize: data => data.attributes, - type: 'authors', + deserialize: data => ({ id: data.id, additionalProperty: `${data.type}-${data.id}` }), + type: 'people', }, } }); @@ -1584,16 +1597,16 @@ describe('JSONAPISerializer', function() { articleAuthor: { data: { type: 'people', - attributes: { firstName: 'Karl' } + id: '1' } } } } }; - const deserializedData = Serializer.deserialize('articles', data); + const deserializedData = Serializer.deserialize('article', data); expect(deserializedData).to.have.property('created_at'); - expect(deserializedData.article_author).to.deep.equal({ first_name: 'Karl' }); + expect(deserializedData.article_author).to.deep.equal({ id: '1', additional_property: 'people-1' }); done(); });