Skip to content

Commit c371495

Browse files
SkysplitSeyZ
authored andcommitted
Fix nested resources loading
1 parent 9a2b1ac commit c371495

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

lib/deserializer-utils.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,33 @@ module.exports = function (jsonapi, data, opts) {
2424
}
2525
}
2626

27-
function findIncluded(relationshipData) {
27+
function findIncluded(relationshipData, from) {
2828
return new Promise(function (resolve) {
29-
if (!jsonapi.included || !relationshipData) { resolve(null); }
29+
if (!jsonapi.included || !relationshipData) { return resolve(null); }
3030

3131
var included = _find(jsonapi.included, {
3232
id: relationshipData.id,
3333
type: relationshipData.type
3434
});
3535

36+
var includedObject = {
37+
to: {
38+
id: from.id,
39+
type: from.type,
40+
},
41+
from: relationshipData,
42+
};
43+
3644
// Check if the include is already processed (prevent circular
3745
// references).
38-
if (alreadyIncluded.indexOf(included) > -1) {
46+
if (_find(alreadyIncluded, includedObject)) {
3947
return resolve(null);
4048
} else {
41-
alreadyIncluded.push(included);
49+
alreadyIncluded.push(includedObject)
50+
alreadyIncluded.push({
51+
to: includedObject.from,
52+
from: includedObject.to,
53+
});
4254
}
4355

4456
if (included) {
@@ -107,13 +119,13 @@ module.exports = function (jsonapi, data, opts) {
107119
} else if (Array.isArray(relationship.data)) {
108120
return Promise
109121
.all(relationship.data.map(function (relationshipData) {
110-
return extractIncludes(relationshipData);
122+
return extractIncludes(relationshipData, from);
111123
}))
112124
.then(function (includes) {
113125
if (includes) { dest[keyForAttribute(key)] = includes; }
114126
});
115127
} else {
116-
return extractIncludes(relationship.data)
128+
return extractIncludes(relationship.data, from)
117129
.then(function (include) {
118130
if (include) { dest[keyForAttribute(key)] = include; }
119131
});
@@ -124,8 +136,8 @@ module.exports = function (jsonapi, data, opts) {
124136
});
125137
}
126138

127-
function extractIncludes(relationshipData) {
128-
return findIncluded(relationshipData)
139+
function extractIncludes(relationshipData, from) {
140+
return findIncluded(relationshipData, from)
129141
.then(function (included) {
130142
var valueForRelationship = getValueForRelationship(relationshipData,
131143
included);

test/deserializer.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,7 @@ describe('JSON API Deserializer', function () {
714714
});
715715

716716
expect(json[0].addresses[2].lock).to.be.eql({
717-
'address-line1': '123 Sth Street',
718-
'zip-code': '12332',
719-
country: 'USA',
720-
id: '54735697e16624ba1eee36cf',
721-
lock: { 'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1' }
717+
'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1'
722718
});
723719

724720
done(null, json);

0 commit comments

Comments
 (0)