diff --git a/lib/serializer-utils.js b/lib/serializer-utils.js index c37520e..3df8dda 100644 --- a/lib/serializer-utils.js +++ b/lib/serializer-utils.js @@ -6,6 +6,7 @@ var _merge = require('lodash/merge'); var _identity = require('lodash/identity'); var _transform = require('lodash/transform'); var _mapValues = require('lodash/mapValues'); +var _reduce = require('lodash/reduce'); var _mapKeys = require('lodash/mapKeys'); var _pick = require('lodash/pick'); var _pickBy = require('lodash/pickBy'); @@ -86,13 +87,18 @@ module.exports = function (collectionName, record, payload, opts) { } function getLinks(current, links, dest) { - return _mapValues(links, function (value) { + return _reduce(links, function(result, value, key){ + let link; if (isFunction(value)) { - return value(record, current, dest); + link = value(record, current, dest); } else { - return value; + link = value; } - }); + if(link !== null){ + result[key] = link; + } + return result; + }, {}) } function getMeta(current, meta) { @@ -158,7 +164,7 @@ module.exports = function (collectionName, record, payload, opts) { if (opts.relationshipLinks) { var links = getLinks(current[attribute], opts.relationshipLinks, dest); - if (links.related) { + if(Object.keys(links).length > 0){ dest.relationships[keyForAttribute(attribute)].links = links; } } diff --git a/test/serializer.js b/test/serializer.js index 22ed0e4..7b4f669 100644 --- a/test/serializer.js +++ b/test/serializer.js @@ -2614,7 +2614,7 @@ describe('JSON API Serializer', function () { }]); }); - it('should not be set when the relationshipLinks return null', function () { + it('should not be set when the related relationshipLinks return null', function () { var dataSet = { id: '54735750e16638ba1eee59cb', firstName: 'Sandro', @@ -2637,5 +2637,179 @@ describe('JSON API Serializer', function () { expect(json.data.relationships.address).eql({ data: null }); }); + + it('should not be set when the self relationshipLinks return null', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + self: function () { + return null; + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ data: null }); + }); + + it('should not be set when the self and related relationshipLinks return null', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + self: function () { + return null; + }, + related: function(){ + return null; + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ data: null }); + }); + + it('should be set when the relationshipLinks returns a self link only', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + self: function(){ + return 'self-relationship-link' + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ + data: null, + links: { + self: 'self-relationship-link' + } + }); + }); + + it('should be set when the relationshipLinks returns a related link only', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + related: function(){ + return 'related-relationship-link' + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ + data: null, + links: { + related: 'related-relationship-link' + } + }); + }); + + it('should be set when the relationshipLinks returns a related link and null self link', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + related: function(){ + return 'related-relationship-link' + }, + self: function(){ + return null + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ + data: null, + links: { + related: 'related-relationship-link' + } + }); + }); + + + it('should be set when the relationshipLinks returns a self link and null related link', function () { + var dataSet = { + id: '54735750e16638ba1eee59cb', + firstName: 'Sandro', + lastName: 'Munda', + address: null, + }; + + var json = new JSONAPISerializer('users', { + attributes: ['firstName', 'lastName', 'address'], + address: { + ref: 'id', + included: false, + relationshipLinks: { + related: function(){ + return null + }, + self: function(){ + return 'self-relationship-link' + } + }, + } + }).serialize(dataSet); + + expect(json.data.relationships.address).eql({ + data: null, + links: { + self: 'self-relationship-link' + } + }); + }); }); });