Skip to content

Commit 9ce1bf0

Browse files
committed
fix(#192): self links not serialized without a related link
1 parent 2c3b80f commit 9ce1bf0

File tree

2 files changed

+159
-6
lines changed

2 files changed

+159
-6
lines changed

lib/serializer-utils.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var _merge = require('lodash/merge');
66
var _identity = require('lodash/identity');
77
var _transform = require('lodash/transform');
88
var _mapValues = require('lodash/mapValues');
9+
var _reduce = require('lodash/reduce');
910
var _mapKeys = require('lodash/mapKeys');
1011
var _pick = require('lodash/pick');
1112
var _pickBy = require('lodash/pickBy');
@@ -86,13 +87,18 @@ module.exports = function (collectionName, record, payload, opts) {
8687
}
8788

8889
function getLinks(current, links, dest) {
89-
return _mapValues(links, function (value) {
90+
return _reduce(links, function(result, value, key){
91+
let link;
9092
if (isFunction(value)) {
91-
return value(record, current, dest);
93+
link = value(record, current, dest);
9294
} else {
93-
return value;
95+
link = value;
9496
}
95-
});
97+
if(link !== null){
98+
result[key] = link;
99+
}
100+
return result;
101+
}, {})
96102
}
97103

98104
function getMeta(current, meta) {
@@ -158,7 +164,7 @@ module.exports = function (collectionName, record, payload, opts) {
158164

159165
if (opts.relationshipLinks) {
160166
var links = getLinks(current[attribute], opts.relationshipLinks, dest);
161-
if (links.related) {
167+
if(Object.keys(links).length > 0){
162168
dest.relationships[keyForAttribute(attribute)].links = links;
163169
}
164170
}

test/serializer.js

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ describe('JSON API Serializer', function () {
26142614
}]);
26152615
});
26162616

2617-
it('should not be set when the relationshipLinks return null', function () {
2617+
it('should not be set when the related relationshipLinks return null', function () {
26182618
var dataSet = {
26192619
id: '54735750e16638ba1eee59cb',
26202620
firstName: 'Sandro',
@@ -2637,5 +2637,152 @@ describe('JSON API Serializer', function () {
26372637

26382638
expect(json.data.relationships.address).eql({ data: null });
26392639
});
2640+
2641+
it('should not be set when the self relationshipLinks return null', function () {
2642+
var dataSet = {
2643+
id: '54735750e16638ba1eee59cb',
2644+
firstName: 'Sandro',
2645+
lastName: 'Munda',
2646+
address: null,
2647+
};
2648+
2649+
var json = new JSONAPISerializer('users', {
2650+
attributes: ['firstName', 'lastName', 'address'],
2651+
address: {
2652+
ref: 'id',
2653+
included: false,
2654+
relationshipLinks: {
2655+
self: function () {
2656+
return null;
2657+
}
2658+
},
2659+
}
2660+
}).serialize(dataSet);
2661+
2662+
expect(json.data.relationships.address).eql({ data: null });
2663+
});
2664+
2665+
it('should be set when the relationshipLinks returns a self link only', function () {
2666+
var dataSet = {
2667+
id: '54735750e16638ba1eee59cb',
2668+
firstName: 'Sandro',
2669+
lastName: 'Munda',
2670+
address: null,
2671+
};
2672+
2673+
var json = new JSONAPISerializer('users', {
2674+
attributes: ['firstName', 'lastName', 'address'],
2675+
address: {
2676+
ref: 'id',
2677+
included: false,
2678+
relationshipLinks: {
2679+
self: function(){
2680+
return 'self-relationship-link'
2681+
}
2682+
},
2683+
}
2684+
}).serialize(dataSet);
2685+
2686+
expect(json.data.relationships.address).eql({
2687+
data: null,
2688+
links: {
2689+
self: 'self-relationship-link'
2690+
}
2691+
});
2692+
});
2693+
2694+
it('should be set when the relationshipLinks returns a related link only', function () {
2695+
var dataSet = {
2696+
id: '54735750e16638ba1eee59cb',
2697+
firstName: 'Sandro',
2698+
lastName: 'Munda',
2699+
address: null,
2700+
};
2701+
2702+
var json = new JSONAPISerializer('users', {
2703+
attributes: ['firstName', 'lastName', 'address'],
2704+
address: {
2705+
ref: 'id',
2706+
included: false,
2707+
relationshipLinks: {
2708+
related: function(){
2709+
return 'related-relationship-link'
2710+
}
2711+
},
2712+
}
2713+
}).serialize(dataSet);
2714+
2715+
expect(json.data.relationships.address).eql({
2716+
data: null,
2717+
links: {
2718+
related: 'related-relationship-link'
2719+
}
2720+
});
2721+
});
2722+
2723+
it('should be set when the relationshipLinks returns a related link and null self link', function () {
2724+
var dataSet = {
2725+
id: '54735750e16638ba1eee59cb',
2726+
firstName: 'Sandro',
2727+
lastName: 'Munda',
2728+
address: null,
2729+
};
2730+
2731+
var json = new JSONAPISerializer('users', {
2732+
attributes: ['firstName', 'lastName', 'address'],
2733+
address: {
2734+
ref: 'id',
2735+
included: false,
2736+
relationshipLinks: {
2737+
related: function(){
2738+
return 'related-relationship-link'
2739+
},
2740+
self: function(){
2741+
return null
2742+
}
2743+
},
2744+
}
2745+
}).serialize(dataSet);
2746+
2747+
expect(json.data.relationships.address).eql({
2748+
data: null,
2749+
links: {
2750+
related: 'related-relationship-link'
2751+
}
2752+
});
2753+
});
2754+
2755+
2756+
it('should be set when the relationshipLinks returns a self link and null related link', function () {
2757+
var dataSet = {
2758+
id: '54735750e16638ba1eee59cb',
2759+
firstName: 'Sandro',
2760+
lastName: 'Munda',
2761+
address: null,
2762+
};
2763+
2764+
var json = new JSONAPISerializer('users', {
2765+
attributes: ['firstName', 'lastName', 'address'],
2766+
address: {
2767+
ref: 'id',
2768+
included: false,
2769+
relationshipLinks: {
2770+
related: function(){
2771+
return null
2772+
},
2773+
self: function(){
2774+
return 'self-relationship-link'
2775+
}
2776+
},
2777+
}
2778+
}).serialize(dataSet);
2779+
2780+
expect(json.data.relationships.address).eql({
2781+
data: null,
2782+
links: {
2783+
self: 'self-relationship-link'
2784+
}
2785+
});
2786+
});
26402787
});
26412788
});

0 commit comments

Comments
 (0)