Skip to content

Commit 404b996

Browse files
authored
fix: remove undefined id on deserialized data when the input has no id (#131)
* test: add a failing test on the presence of an `id` property on deserialized data when the input has no `id` * fix: remove `id: undefined` from deserialized output when the input data had no `id`
1 parent 677ba24 commit 404b996

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: lib/JSONAPISerializer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,9 @@ module.exports = class JSONAPISerializer {
385385
const options = this.schemas[type][schema];
386386

387387
let deserializedData = {};
388-
deserializedData[options.id] = data.id || undefined;
388+
if (data.id !== undefined) {
389+
deserializedData[options.id] = data.id;
390+
}
389391

390392
if (data.attributes && options.whitelistOnDeserialize.length) {
391393
data.attributes = pick(data.attributes, options.whitelistOnDeserialize);

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

+16
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,22 @@ describe('JSONAPISerializer', function() {
29612961

29622962
done();
29632963
});
2964+
2965+
it("should return deserialized data without 'id' for a single data without 'id'", function(done) {
2966+
const singleData = {
2967+
data: {
2968+
type: 'article',
2969+
attributes: {
2970+
title: 'JSON API paints my bikeshed!',
2971+
}
2972+
}
2973+
};
2974+
const deserializedData = Serializer.deserialize(typeOption, singleData);
2975+
2976+
expect(deserializedData).to.not.have.property('id');
2977+
expect(deserializedData).to.have.property('title').to.eql('JSON API paints my bikeshed!');
2978+
done();
2979+
});
29642980
});
29652981

29662982
describe('serializeError', function() {

0 commit comments

Comments
 (0)