Skip to content

Commit

Permalink
relationship data key should be undefined for undefined relationship
Browse files Browse the repository at this point in the history
closes #13
  • Loading branch information
danivek committed Oct 28, 2016
1 parent 135949e commit 12bc2cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ module.exports = class JSONAPISerializer {
* @return {Object|Object[]} serialized relationship data.
*/
serializeRelationship(rType, rData, rOptions, included) {
// No relationship data
if (rData === undefined) {
return undefined;
}

// Empty relationship data
if (!_.isNumber(rData) && _.isEmpty(rData)) {
// Return [] or null
Expand Down
6 changes: 6 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ describe('JSONAPISerializer', function() {
},
});

it('should return undefined for an undefined relationship data', function(done) {
const serializedRelationshipData = Serializer.serializeRelationship('articles', undefined);
expect(serializedRelationshipData).to.eql(undefined);
done();
});

it('should return null for an empty single relationship data', function(done) {
const serializedRelationshipData = Serializer.serializeRelationship('articles', {});
expect(serializedRelationshipData).to.eql(null);
Expand Down

0 comments on commit 12bc2cd

Please sign in to comment.