Skip to content

Commit c5b6ea5

Browse files
Vincent MoliniéSeyZ
authored andcommitted
[*] Serializer - FIx serialization if id is 0
1 parent 4e70016 commit c5b6ea5

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/serializer-utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var isPlainObject = require('lodash/isPlainObject');
33
var isFunction = require('lodash/isFunction');
44
var _find = require('lodash/find');
55
var _merge = require('lodash/merge');
6-
var _defaultsDeep = require('lodash/defaultsDeep');
76
var _identity = require('lodash/identity');
87
var _transform = require('lodash/transform');
98
var _mapValues = require('lodash/mapValues');
@@ -12,7 +11,7 @@ var _pick = require('lodash/pick');
1211
var _pickBy = require('lodash/pickBy');
1312
var _keys = require('lodash/keys');
1413
var _each = require('lodash/each');
15-
var _isUndefined = require('lodash/isUndefined');
14+
var _isNil = require('lodash/isNil');
1615
var Inflector = require('./inflector');
1716

1817
module.exports = function (collectionName, record, payload, opts) {
@@ -278,7 +277,7 @@ module.exports = function (collectionName, record, payload, opts) {
278277

279278
// Top-level data.
280279
var data = { type: getType(collectionName, record) };
281-
if (record[getId()]) { data.id = String(record[getId()]); }
280+
if (!_isNil(record[getId()])) { data.id = String(record[getId()]); }
282281

283282
// Data links.
284283
if (opts.dataLinks) {

test/serializer.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ describe('Options', function () {
7272
expect(json.data.id).to.equal('123');
7373
done(null, json);
7474
});
75+
76+
it('should be serialized when it\'s 0', function (done) {
77+
var dataSet = {
78+
id: 0,
79+
firstName: 'Sandro',
80+
lastName: 'Munda'
81+
};
82+
83+
var json = new JSONAPISerializer('user', {
84+
attributes: ['firstName', 'lastName'],
85+
}).serialize(dataSet);
86+
87+
expect(json.data.id).equal('0');
88+
done(null, json);
89+
});
7590
});
7691

7792
describe('pluralizeType', function () {
@@ -342,7 +357,7 @@ describe('Options', function () {
342357

343358
var json = new JSONAPISerializer('user', {
344359
attributes: ['firstName', 'lastName'],
345-
dataMeta: function (record) { return { copyright: record.firstName + ' ' + record.lastName }; }
360+
dataMeta: function (record) { return { copyright: record.firstName + ' ' + record.lastName }; }
346361
}).serialize(dataSet);
347362

348363
expect(json.data[0].meta.copyright).equal("Sandro Munda");

0 commit comments

Comments
 (0)