Skip to content

Commit 623b8e4

Browse files
authored
fix: correct error when there is a relationship for included document with unknown type (#126)
1 parent 631ce31 commit 623b8e4

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Diff for: lib/JSONAPISerializer.js

+4
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ module.exports = class JSONAPISerializer {
560560
return deserializeFunction({ type, id });
561561
}
562562

563+
if (!relationshipOpts) {
564+
throw new Error(`No type registered for ${type}`);
565+
}
566+
563567
return this.deserializeResource(
564568
type,
565569
includedResource,

Diff for: test/unit/JSONAPISerializer.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,70 @@ describe('JSONAPISerializer', function() {
24952495
done();
24962496
});
24972497

2498+
it('should throw an error if type has not been registered for a relationship of included relationship', function(done) {
2499+
const Serializer = new JSONAPISerializer();
2500+
Serializer.register('article', {
2501+
relationships: {
2502+
author: {
2503+
type: 'people'
2504+
}
2505+
}
2506+
});
2507+
2508+
Serializer.register('people', {});
2509+
2510+
const data = {
2511+
data: {
2512+
id: '1',
2513+
type: 'article',
2514+
attributes: {
2515+
title: 'JSON API paints my bikeshed!',
2516+
body: 'The shortest article. Ever.',
2517+
created: '2015-05-22T14:56:29.000Z'
2518+
},
2519+
relationships: {
2520+
author: {
2521+
data: {
2522+
type: 'people',
2523+
id: '1'
2524+
}
2525+
},
2526+
}
2527+
},
2528+
included: [
2529+
{
2530+
type: 'people',
2531+
id: '1',
2532+
attributes: {
2533+
firstName: 'Kaley',
2534+
lastName: 'Maggio',
2535+
2536+
age: '80',
2537+
gender: 'male'
2538+
},
2539+
relationships: {
2540+
profile: {
2541+
data: {
2542+
type: 'profile',
2543+
id: '1'
2544+
}
2545+
}
2546+
}
2547+
},
2548+
{
2549+
type: 'profile',
2550+
id: '1',
2551+
attributes: {}
2552+
}
2553+
]
2554+
};
2555+
2556+
expect(function() {
2557+
Serializer.deserialize('article', data);
2558+
}).to.throw(Error, 'No type registered for profile');
2559+
done();
2560+
});
2561+
24982562
it('should deserialize with a custom schema', function(done) {
24992563
const Serializer = new JSONAPISerializer();
25002564
Serializer.register('articles', 'custom');

0 commit comments

Comments
 (0)