Skip to content

Commit 392ec87

Browse files
committed
Allow null ID for data creation. Closes #70
1 parent 85d9068 commit 392ec87

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

lib/serializer-utils.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,8 @@ module.exports = function (collectionName, record, payload, opts) {
264264
}
265265

266266
// Top-level data.
267-
var data = {
268-
type: getType(collectionName, record),
269-
id: String(record[getId()])
270-
};
267+
var data = { type: getType(collectionName, record) };
268+
if (record[getId()]) { data.id = record[getId()]; }
271269

272270
// Data links.
273271
if (opts.dataLinks) {

test/serializer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ describe('Options', function () {
2828
expect(json.data[0].id).equal('54735750e16638ba1eee59cb');
2929
done(null, json);
3030
});
31+
32+
it('should not be serialized when it\'s null', function (done) {
33+
var dataSet = {
34+
id: null,
35+
firstName: 'Sandro',
36+
lastName: 'Munda'
37+
};
38+
39+
var json = new JSONAPISerializer('user', {
40+
attributes: ['firstName', 'lastName'],
41+
}).serialize(dataSet);
42+
43+
expect(json.data).to.not.have.keys('id');
44+
done(null, json);
45+
});
46+
47+
it('should not be serialized when it\'s undefined', function (done) {
48+
var dataSet = {
49+
firstName: 'Sandro',
50+
lastName: 'Munda'
51+
};
52+
53+
var json = new JSONAPISerializer('user', {
54+
attributes: ['firstName', 'lastName'],
55+
}).serialize(dataSet);
56+
57+
expect(json.data).to.not.have.keys('id');
58+
done(null, json);
59+
});
3160
});
3261

3362
describe('pluralizeType', function () {

0 commit comments

Comments
 (0)