Skip to content

Commit

Permalink
Empty relationships returned as data: null
Browse files Browse the repository at this point in the history
closes #41
  • Loading branch information
danivek committed Jan 15, 2018
1 parent ad6ca64 commit 0c7588b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/JSONAPISerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,19 @@ module.exports = class JSONAPISerializer {
throw new Error(`No schema "${rOptions.schema}" registered for type "${rOptions.type}" on "${relationship}" relationship`);
}

const serializeRelationship = {
let serializeRelationship = {
links: this.processOptionsValues(data, extraData, rOptions.links),
meta: this.processOptionsValues(data, extraData, rOptions.meta),
data: this.serializeRelationship(rOptions.type, data[relationshipKey], this.schemas[rOptions.type][schema], included, extraData),
};

// Avoid empty relationship object
if (serializeRelationship.data === undefined && serializeRelationship.links === undefined && serializeRelationship.meta === undefined) {
serializeRelationship = {
data: null,
};
}

relationship = (options.convertCase) ? this._convertCase(relationship, options.convertCase) : relationship;

_.set(serializedRelationships, relationship, serializeRelationship);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/JSONAPISerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ describe('JSONAPISerializer', function() {
done();
});

it('should return at least data null if no links, data, or meta are deduce', function(done) {
const included = [];
const serializedRelationships = Serializer.serializeRelationships({
id: '1',
}, Serializer.schemas.articles.default, included);
expect(serializedRelationships).to.eql({
author: {
data: null
},
comments: {
data: null
}
})
done();
});

it('should return relationships for author and comments', function(done) {
const included = [];
const serializedRelationships = Serializer.serializeRelationships({
Expand Down

0 comments on commit 0c7588b

Please sign in to comment.