Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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<string, object>} included Included resources.
* @param {object} extraData additional data.
* @param {Map<string, object>} [included] Included resources.
* @param {object} [extraData] additional data.
* @returns {object|object[]} serialized data.
*/
serializeResource(type, data, options, included, extraData) {
Expand Down Expand Up @@ -521,8 +521,8 @@ module.exports = class JSONAPISerializer {
* @private
* @param {object} typeOption a dynamic type options.
* @param {object|object[]} data input data.
* @param {Map<string, object>} included Included resources.
* @param {object} extraData additional data.
* @param {Map<string, object>} [included] Included resources.
* @param {object} [extraData] additional data.
* @returns {object|object[]} serialized data.
*/
serializeMixedResource(typeOption, data, included, extraData) {
Expand Down Expand Up @@ -597,8 +597,8 @@ module.exports = class JSONAPISerializer {
* @private
* @param {object|object[]} data input data.
* @param {object} options resource's configuration options.
* @param {Map<string, object>} included Included resources.
* @param {object} extraData additional data.
* @param {Map<string, object>} [included] Included resources.
* @param {object} [extraData] additional data.
* @returns {object} serialized relationships.
*/
serializeRelationships(data, options, included, extraData) {
Expand Down Expand Up @@ -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<string, object>} included Included resources.
* @param {object} data the entire resource's data.
* @param {object} extraData additional data.
* @param {Map<string, object>} [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) {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down