Skip to content

Commit

Permalink
Serialize data: null when relationship is null
Browse files Browse the repository at this point in the history
  • Loading branch information
SeyZ committed May 14, 2016
1 parent c364e9b commit f692524
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
12 changes: 5 additions & 7 deletions lib/serializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module.exports = function (collectionName, record, payload, opts) {
return item.map(function (val) {
return String(val);
});
} else {
} else if (item) {
return String(item);
}
} else {
} else if (item && item[opts.ref]){
return String(item[opts.ref]);
}
}
Expand Down Expand Up @@ -187,13 +187,11 @@ module.exports = function (collectionName, record, payload, opts) {
if (opts.includedLinks) {
included.links = getLinks(dest, opts.includedLinks);
}
// only add to included array if the resource is defined
if(id !== 'undefined') {
pushToIncluded(payload, included);
}

if (typeof id !== 'undefined') { pushToIncluded(payload, included); }
}

return id !== 'undefined' ? { type: type, id: id } : null;
return typeof id !== 'undefined' ? { type: type, id: id } : null;
};

this.serializeNested = function (dest, current, attribute, opts) {
Expand Down
23 changes: 22 additions & 1 deletion test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,28 @@ describe('JSON API Serializer', function () {
});
});

describe('Null relationship', function () {
it('should serialize the relationship as { data: null }', function (done) {
var dataSet = {
id: '54735750e16638ba1eee59cb',
firstName: 'Sandro',
lastName: 'Munda',
address: null,
};

var json = new JSONAPISerializer('user', {
attributes: ['firstName', 'lastName', 'address'],
address: {
ref: 'id',
included: false
}
}).serialize(dataSet);

expect(json.data.relationships.address).eql({ data: null });
done(null, json);
});
});

describe('Nested of nested document', function () {
it('should be serialized', function (done) {
var dataSet = {
Expand Down Expand Up @@ -1910,5 +1932,4 @@ describe('JSON API Serializer', function () {
done(null, json);
});
});

});

0 comments on commit f692524

Please sign in to comment.