Skip to content

Commit 3d776ef

Browse files
VincentMolinieSeyZ
authored andcommitted
Don't set the relationshipLinks when it returns null
1 parent 20798d9 commit 3d776ef

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

lib/serializer-utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ module.exports = function (collectionName, record, payload, opts) {
154154
}
155155

156156
if (opts.relationshipLinks) {
157-
dest.relationships[keyForAttribute(attribute)].links =
158-
getLinks(current[attribute], opts.relationshipLinks, dest);
157+
const links = getLinks(current[attribute], opts.relationshipLinks, dest);
158+
if (links.related) {
159+
dest.relationships[keyForAttribute(attribute)].links = links;
160+
}
159161
}
160162

161163
if (opts.relationshipMeta) {

test/serializer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,5 +2559,29 @@ describe('JSON API Serializer', function () {
25592559
relationships: { publisher: { data: null, links: { related: 'bar' } } }
25602560
}]);
25612561
});
2562+
2563+
it('should not be set when the relationshipLinks return null', function () {
2564+
var dataSet = {
2565+
id: '54735750e16638ba1eee59cb',
2566+
firstName: 'Sandro',
2567+
lastName: 'Munda',
2568+
address: null,
2569+
};
2570+
2571+
var json = new JSONAPISerializer('users', {
2572+
attributes: ['firstName', 'lastName', 'address'],
2573+
address: {
2574+
ref: 'id',
2575+
included: false,
2576+
relationshipLinks: {
2577+
related: function () {
2578+
return null;
2579+
}
2580+
},
2581+
}
2582+
}).serialize(dataSet);
2583+
2584+
expect(json.data.relationships.address).eql({ data: null });
2585+
});
25622586
});
25632587
});

0 commit comments

Comments
 (0)