Skip to content

Commit

Permalink
fix issue with falsy attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
tonylukasavage committed Feb 26, 2016
1 parent 927c31a commit 1bc3dea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/serializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ module.exports = function (collectionName, record, payload, opts) {
}

_.each(opts.attributes, function (attribute) {
if (record[attribute]) {
if (attribute in record) {
if (!data.attributes) { data.attributes = {}; }
that.serialize(data, record, attribute, opts[attribute]);
} else {
Expand Down
26 changes: 26 additions & 0 deletions test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1662,4 +1662,30 @@ describe('JSON API Serializer', function () {
expect(json.data.attributes).to.have.property('last-name');
});
});

describe('falsy attribute values', function() {
it('properly attach falsy attributes', function() {
var dataSet = {
id: '1',
count: 0,
bool: false,
dbNull: null,
emptyString: ''
};

var json = new JSONAPISerializer('tester', {
attributes: ['count', 'bool', 'dbNull', 'emptyString']
}).serialize(dataSet);

expect(json.data.attributes).to.have.property('count');
expect(json.data.attributes.count).to.equal(0);
expect(json.data.attributes).to.have.property('bool');
expect(json.data.attributes.bool).to.equal(false);
expect(json.data.attributes).to.have.property('db-null');
expect(json.data.attributes['db-null']).to.equal(null);
expect(json.data.attributes).to.have.property('empty-string');
expect(json.data.attributes['empty-string']).to.equal('');
});
});

});

0 comments on commit 1bc3dea

Please sign in to comment.