Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/serializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
176 changes: 175 additions & 1 deletion test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'
}
});
});
});
});