Skip to content

Commit ac89df5

Browse files
committed
Fix bug with included and deserialize function
1 parent fa3f6c5 commit ac89df5

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

Diff for: lib/JSONAPISerializer.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ module.exports = class JSONAPISerializer {
494494
relationshipData.id,
495495
relationshipOptions,
496496
included,
497-
lineageCopy
497+
lineageCopy,
498+
deserializeFunction
498499
);
499500
};
500501

@@ -548,15 +549,16 @@ module.exports = class JSONAPISerializer {
548549
* @param {RelationshipOptions} relationshipOpts relationship option.
549550
* @param {Map<string, object>} included Included resources.
550551
* @param {string[]} lineage resource identifiers already deserialized to prevent circular references.
552+
* @param {Function} deserializeFunction a deserialize function
551553
* @returns {object} deserialized data.
552554
*/
553-
deserializeIncluded(type, id, relationshipOpts, included, lineage) {
555+
deserializeIncluded(type, id, relationshipOpts, included, lineage, deserializeFunction) {
554556
const includedResource = included.find(
555557
(resource) => resource.type === type && resource.id === id
556558
);
557559

558560
if (!includedResource) {
559-
return id;
561+
return deserializeFunction({ type, id });
560562
}
561563

562564
return this.deserializeResource(

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

+43-6
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,43 @@ describe('JSONAPISerializer', function() {
17691769
done();
17701770
});
17711771

1772+
it('should deserialize with missing included relationship and deserialize function', function(done) {
1773+
const Serializer = new JSONAPISerializer();
1774+
Serializer.register('articles', {
1775+
relationships: {
1776+
author: {
1777+
type: 'people',
1778+
deserialize: data => ({ id: data.id })
1779+
}
1780+
}
1781+
});
1782+
Serializer.register('people', {});
1783+
1784+
const data = {
1785+
data: {
1786+
type: 'article',
1787+
id: '1',
1788+
attributes: {
1789+
title: 'JSON API paints my bikeshed!',
1790+
},
1791+
relationships: {
1792+
author: {
1793+
data: {
1794+
type: 'people',
1795+
id: '1'
1796+
}
1797+
}
1798+
}
1799+
},
1800+
included: []
1801+
}
1802+
1803+
const deserializedData = Serializer.deserialize('articles', data);
1804+
// People with id '1' is missing in included
1805+
expect(deserializedData).to.have.property('author').to.deep.eql({ id: '1' });
1806+
done();
1807+
});
1808+
17721809
it('should deserialize an array of data', function(done) {
17731810
const Serializer = new JSONAPISerializer();
17741811
Serializer.register('articles', {});
@@ -2083,7 +2120,7 @@ describe('JSONAPISerializer', function() {
20832120

20842121
it('should deserializing relationship when alternativeKey is set and relationship data is not in the included array', function (done) {
20852122
const Serializer = new JSONAPISerializer();
2086-
2123+
20872124
Serializer.register('article', {
20882125
id: 'id',
20892126
relationships: {
@@ -2093,7 +2130,7 @@ describe('JSONAPISerializer', function() {
20932130
},
20942131
},
20952132
});
2096-
2133+
20972134
Serializer.register('people', {
20982135
id: 'id',
20992136
relationships: {
@@ -2103,9 +2140,9 @@ describe('JSONAPISerializer', function() {
21032140
},
21042141
},
21052142
});
2106-
2143+
21072144
Serializer.register('role', {});
2108-
2145+
21092146
const data = {
21102147
data: {
21112148
type: 'article',
@@ -2143,14 +2180,14 @@ describe('JSONAPISerializer', function() {
21432180
},
21442181
],
21452182
};
2146-
2183+
21472184
const deserializedData = Serializer.deserialize('article', data);
21482185
expect(deserializedData).to.have.property('author_id').to.eql('1');
21492186
expect(deserializedData).to.have.property('author');
21502187
expect(deserializedData.author).to.have.property('role_id').to.eql('1');
21512188
expect(deserializedData.author).to.not.have.property('role');
21522189
done();
2153-
});
2190+
});
21542191

21552192
it('should deserialize with \'deserialize\' option as a function', function(done) {
21562193
const Serializer = new JSONAPISerializer();

0 commit comments

Comments
 (0)