diff --git a/lib/JSONAPISerializer.js b/lib/JSONAPISerializer.js index ba71806..83ee9a1 100644 --- a/lib/JSONAPISerializer.js +++ b/lib/JSONAPISerializer.js @@ -113,7 +113,7 @@ module.exports = class JSONAPISerializer { * * @see {@link http://jsonapi.org/format/#document-top-level} * @function JSONAPISerializer#serializeAsync - * @param {string} type resource's type. + * @param {string|object} type resource's type or an object with a dynamic type resolved from data.. * @param {object|object[]} data input data. * @param {string} [schema='default'] resource's schema name. * @param {object} [extraData] additional data that can be used in topLevelMeta options. @@ -362,18 +362,18 @@ module.exports = class JSONAPISerializer { deserializeResource(type, data, schema, included) { if (typeof type === 'object') { type = typeof type.type === 'function' ? type.type(data) : get(data, type.type); + } - if (!type) { - throw new Error(`No type can be resolved from data: ${JSON.stringify(data)}`); - } - - if (!this.schemas[type]) { - throw new Error(`No type registered for ${type}`); - } + if (!type) { + throw new Error(`No type can be resolved from data: ${JSON.stringify(data)}`); + } - schema = 'default'; + if (!this.schemas[type]) { + throw new Error(`No type registered for ${type}`); } + schema = 'default'; + const options = this.schemas[type][schema]; let deserializedData = {}; @@ -489,8 +489,8 @@ module.exports = class JSONAPISerializer { * @param {string} type resource's type. * @param {object|object[]} data input data. * @param {object} options resource's configuration options. - * @param {Map} included Included resources. - * @param {object} extraData additional data. + * @param {Map} [included] Included resources. + * @param {object} [extraData] additional data. * @returns {object|object[]} serialized data. */ serializeResource(type, data, options, included, extraData) { @@ -521,8 +521,8 @@ module.exports = class JSONAPISerializer { * @private * @param {object} typeOption a dynamic type options. * @param {object|object[]} data input data. - * @param {Map} included Included resources. - * @param {object} extraData additional data. + * @param {Map} [included] Included resources. + * @param {object} [extraData] additional data. * @returns {object|object[]} serialized data. */ serializeMixedResource(typeOption, data, included, extraData) { @@ -597,8 +597,8 @@ module.exports = class JSONAPISerializer { * @private * @param {object|object[]} data input data. * @param {object} options resource's configuration options. - * @param {Map} included Included resources. - * @param {object} extraData additional data. + * @param {Map} [included] Included resources. + * @param {object} [extraData] additional data. * @returns {object} serialized relationships. */ serializeRelationships(data, options, included, extraData) { @@ -657,9 +657,9 @@ module.exports = class JSONAPISerializer { * @param {string|Function} rType the relationship's type. * @param {string} rSchema the relationship's schema * @param {object|object[]} rData relationship's data. - * @param {Map} included Included resources. - * @param {object} data the entire resource's data. - * @param {object} extraData additional data. + * @param {Map} [included] Included resources. + * @param {object} [data] the entire resource's data. + * @param {object} [extraData] additional data. * @returns {object|object[]} serialized relationship data. */ serializeRelationship(rType, rSchema, rData, included, data, extraData) { diff --git a/test/unit/JSONAPISerializer.test.js b/test/unit/JSONAPISerializer.test.js index d7230dc..72ede26 100644 --- a/test/unit/JSONAPISerializer.test.js +++ b/test/unit/JSONAPISerializer.test.js @@ -1646,6 +1646,53 @@ describe('JSONAPISerializer', function() { done(); }); + it('should throw an error if type has not been registered for included relationship', function(done) { + const Serializer = new JSONAPISerializer(); + Serializer.register('article', { + relationships: { + author: { + type: 'people' + } + } + }); + + const data = { + data: { + id: '1', + type: 'article', + 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: { + firstName: 'Kaley', + lastName: 'Maggio', + email: 'Kaley-Maggio@example.com', + age: '80', + gender: 'male' + } + }] + }; + + expect(function() { + Serializer.deserialize('article', data); + }).to.throw(Error, 'No type registered for people'); + done(); + }); + it('should throw an error if custom schema has not been registered', function(done) { expect(function() { const Serializer = new JSONAPISerializer();