Skip to content

Commit 50de26b

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

File tree

2 files changed

+186
-6
lines changed

2 files changed

+186
-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: 175 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,179 @@ 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 not be set when the self and related relationshipLinks return null', 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 null;
2681+
},
2682+
related: function(){
2683+
return null;
2684+
}
2685+
},
2686+
}
2687+
}).serialize(dataSet);
2688+
2689+
expect(json.data.relationships.address).eql({ data: null });
2690+
});
2691+
2692+
it('should be set when the relationshipLinks returns a self link only', function () {
2693+
var dataSet = {
2694+
id: '54735750e16638ba1eee59cb',
2695+
firstName: 'Sandro',
2696+
lastName: 'Munda',
2697+
address: null,
2698+
};
2699+
2700+
var json = new JSONAPISerializer('users', {
2701+
attributes: ['firstName', 'lastName', 'address'],
2702+
address: {
2703+
ref: 'id',
2704+
included: false,
2705+
relationshipLinks: {
2706+
self: function(){
2707+
return 'self-relationship-link'
2708+
}
2709+
},
2710+
}
2711+
}).serialize(dataSet);
2712+
2713+
expect(json.data.relationships.address).eql({
2714+
data: null,
2715+
links: {
2716+
self: 'self-relationship-link'
2717+
}
2718+
});
2719+
});
2720+
2721+
it('should be set when the relationshipLinks returns a related link only', function () {
2722+
var dataSet = {
2723+
id: '54735750e16638ba1eee59cb',
2724+
firstName: 'Sandro',
2725+
lastName: 'Munda',
2726+
address: null,
2727+
};
2728+
2729+
var json = new JSONAPISerializer('users', {
2730+
attributes: ['firstName', 'lastName', 'address'],
2731+
address: {
2732+
ref: 'id',
2733+
included: false,
2734+
relationshipLinks: {
2735+
related: function(){
2736+
return 'related-relationship-link'
2737+
}
2738+
},
2739+
}
2740+
}).serialize(dataSet);
2741+
2742+
expect(json.data.relationships.address).eql({
2743+
data: null,
2744+
links: {
2745+
related: 'related-relationship-link'
2746+
}
2747+
});
2748+
});
2749+
2750+
it('should be set when the relationshipLinks returns a related link and null self link', function () {
2751+
var dataSet = {
2752+
id: '54735750e16638ba1eee59cb',
2753+
firstName: 'Sandro',
2754+
lastName: 'Munda',
2755+
address: null,
2756+
};
2757+
2758+
var json = new JSONAPISerializer('users', {
2759+
attributes: ['firstName', 'lastName', 'address'],
2760+
address: {
2761+
ref: 'id',
2762+
included: false,
2763+
relationshipLinks: {
2764+
related: function(){
2765+
return 'related-relationship-link'
2766+
},
2767+
self: function(){
2768+
return null
2769+
}
2770+
},
2771+
}
2772+
}).serialize(dataSet);
2773+
2774+
expect(json.data.relationships.address).eql({
2775+
data: null,
2776+
links: {
2777+
related: 'related-relationship-link'
2778+
}
2779+
});
2780+
});
2781+
2782+
2783+
it('should be set when the relationshipLinks returns a self link and null related link', function () {
2784+
var dataSet = {
2785+
id: '54735750e16638ba1eee59cb',
2786+
firstName: 'Sandro',
2787+
lastName: 'Munda',
2788+
address: null,
2789+
};
2790+
2791+
var json = new JSONAPISerializer('users', {
2792+
attributes: ['firstName', 'lastName', 'address'],
2793+
address: {
2794+
ref: 'id',
2795+
included: false,
2796+
relationshipLinks: {
2797+
related: function(){
2798+
return null
2799+
},
2800+
self: function(){
2801+
return 'self-relationship-link'
2802+
}
2803+
},
2804+
}
2805+
}).serialize(dataSet);
2806+
2807+
expect(json.data.relationships.address).eql({
2808+
data: null,
2809+
links: {
2810+
self: 'self-relationship-link'
2811+
}
2812+
});
2813+
});
26402814
});
26412815
});

0 commit comments

Comments
 (0)