Skip to content

Fix bug with included and deserialize function #123

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
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
8 changes: 5 additions & 3 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ module.exports = class JSONAPISerializer {
relationshipData.id,
relationshipOptions,
included,
lineageCopy
lineageCopy,
deserializeFunction
);
};

Expand Down Expand Up @@ -548,15 +549,16 @@ module.exports = class JSONAPISerializer {
* @param {RelationshipOptions} relationshipOpts relationship option.
* @param {Map<string, object>} included Included resources.
* @param {string[]} lineage resource identifiers already deserialized to prevent circular references.
* @param {Function} deserializeFunction a deserialize function
* @returns {object} deserialized data.
*/
deserializeIncluded(type, id, relationshipOpts, included, lineage) {
deserializeIncluded(type, id, relationshipOpts, included, lineage, deserializeFunction) {
const includedResource = included.find(
(resource) => resource.type === type && resource.id === id
);

if (!includedResource) {
return id;
return deserializeFunction({ type, id });
}

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

it('should deserialize with missing included relationship and deserialize function', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {
relationships: {
author: {
type: 'people',
deserialize: data => ({ id: data.id })
}
}
});
Serializer.register('people', {});

const data = {
data: {
type: 'article',
id: '1',
attributes: {
title: 'JSON API paints my bikeshed!',
},
relationships: {
author: {
data: {
type: 'people',
id: '1'
}
}
}
},
included: []
}

const deserializedData = Serializer.deserialize('articles', data);
// People with id '1' is missing in included
expect(deserializedData).to.have.property('author').to.deep.eql({ id: '1' });
done();
});

it('should deserialize an array of data', function(done) {
const Serializer = new JSONAPISerializer();
Serializer.register('articles', {});
Expand Down Expand Up @@ -2083,7 +2120,7 @@ describe('JSONAPISerializer', function() {

it('should deserializing relationship when alternativeKey is set and relationship data is not in the included array', function (done) {
const Serializer = new JSONAPISerializer();

Serializer.register('article', {
id: 'id',
relationships: {
Expand All @@ -2093,7 +2130,7 @@ describe('JSONAPISerializer', function() {
},
},
});

Serializer.register('people', {
id: 'id',
relationships: {
Expand All @@ -2103,9 +2140,9 @@ describe('JSONAPISerializer', function() {
},
},
});

Serializer.register('role', {});

const data = {
data: {
type: 'article',
Expand Down Expand Up @@ -2143,14 +2180,14 @@ describe('JSONAPISerializer', function() {
},
],
};

const deserializedData = Serializer.deserialize('article', data);
expect(deserializedData).to.have.property('author_id').to.eql('1');
expect(deserializedData).to.have.property('author');
expect(deserializedData.author).to.have.property('role_id').to.eql('1');
expect(deserializedData.author).to.not.have.property('role');
done();
});
});

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