Skip to content

Correct error when there is a relationship for included document with unknown type #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
4 changes: 4 additions & 0 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ module.exports = class JSONAPISerializer {
return deserializeFunction({ type, id });
}

if (!relationshipOpts) {
throw new Error(`No type registered for ${type}`);
}

return this.deserializeResource(
type,
includedResource,
Expand Down
64 changes: 64 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,70 @@ describe('JSONAPISerializer', function() {
done();
});

it('should throw an error if type has not been registered for a relationship of included relationship', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('article', {
relationships: {
author: {
type: 'people'
}
}
});

Serializer.register('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: '[email protected]',
age: '80',
gender: 'male'
},
relationships: {
profile: {
data: {
type: 'profile',
id: '1'
}
}
}
},
{
type: 'profile',
id: '1',
attributes: {}
}
]
};

expect(function() {
Serializer.deserialize('article', data);
}).to.throw(Error, 'No type registered for profile');
done();
});

it('should deserialize with a custom schema', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', 'custom');
Expand Down