Skip to content

Commit

Permalink
fix: remove undefined id on deserialized data when the input has no…
Browse files Browse the repository at this point in the history
… `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`
  • Loading branch information
medfreeman authored Oct 26, 2021
1 parent 677ba24 commit 404b996
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ module.exports = class JSONAPISerializer {
const options = this.schemas[type][schema];

let deserializedData = {};
deserializedData[options.id] = data.id || undefined;
if (data.id !== undefined) {
deserializedData[options.id] = data.id;
}

if (data.attributes && options.whitelistOnDeserialize.length) {
data.attributes = pick(data.attributes, options.whitelistOnDeserialize);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,22 @@ describe('JSONAPISerializer', function() {

done();
});

it("should return deserialized data without 'id' for a single data without 'id'", function(done) {
const singleData = {
data: {
type: 'article',
attributes: {
title: 'JSON API paints my bikeshed!',
}
}
};
const deserializedData = Serializer.deserialize(typeOption, singleData);

expect(deserializedData).to.not.have.property('id');
expect(deserializedData).to.have.property('title').to.eql('JSON API paints my bikeshed!');
done();
});
});

describe('serializeError', function() {
Expand Down

0 comments on commit 404b996

Please sign in to comment.