Skip to content

Commit a2c77c5

Browse files
SkysplitSeyZ
authored andcommitted
Allow shallow circular dependency to fix multiple relations
1 parent c371495 commit a2c77c5

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

lib/deserializer-utils.js

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

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

@@ -38,19 +38,16 @@ module.exports = function (jsonapi, data, opts) {
3838
id: from.id,
3939
type: from.type,
4040
},
41-
from: relationshipData,
41+
from: Object.assign({}, relationshipData),
42+
relation: relationshipName,
4243
};
4344

4445
// Check if the include is already processed (prevent circular
4546
// references).
4647
if (_find(alreadyIncluded, includedObject)) {
4748
return resolve(null);
4849
} else {
49-
alreadyIncluded.push(includedObject)
50-
alreadyIncluded.push({
51-
to: includedObject.from,
52-
from: includedObject.to,
53-
});
50+
alreadyIncluded.push(includedObject);
5451
}
5552

5653
if (included) {
@@ -119,13 +116,13 @@ module.exports = function (jsonapi, data, opts) {
119116
} else if (Array.isArray(relationship.data)) {
120117
return Promise
121118
.all(relationship.data.map(function (relationshipData) {
122-
return extractIncludes(relationshipData, from);
119+
return extractIncludes(relationshipData, key, from);
123120
}))
124121
.then(function (includes) {
125122
if (includes) { dest[keyForAttribute(key)] = includes; }
126123
});
127124
} else {
128-
return extractIncludes(relationship.data, from)
125+
return extractIncludes(relationship.data, key, from)
129126
.then(function (include) {
130127
if (include) { dest[keyForAttribute(key)] = include; }
131128
});
@@ -136,8 +133,8 @@ module.exports = function (jsonapi, data, opts) {
136133
});
137134
}
138135

139-
function extractIncludes(relationshipData, from) {
140-
return findIncluded(relationshipData, from)
136+
function extractIncludes(relationshipData, relationshipName, from) {
137+
return findIncluded(relationshipData, relationshipName, from)
141138
.then(function (included) {
142139
var valueForRelationship = getValueForRelationship(relationshipData,
143140
included);

test/deserializer.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,50 @@ describe('JSON API Deserializer', function () {
10461046

10471047
});
10481048
});
1049+
1050+
describe('With multiple relations', function () {
1051+
it('should include both relations if they point to same include', function (done) {
1052+
var dataSet = {
1053+
data: {
1054+
type: 'posts',
1055+
id: 1,
1056+
relationships: {
1057+
owner: {
1058+
data: {
1059+
type: 'users',
1060+
id: 1,
1061+
},
1062+
},
1063+
publisher: {
1064+
data: {
1065+
type: 'users',
1066+
id: 1,
1067+
},
1068+
},
1069+
},
1070+
},
1071+
included: [
1072+
{
1073+
type: 'users',
1074+
id: 1,
1075+
attributes: {
1076+
'first-name': 'Sandro',
1077+
'last-name': 'Munda',
1078+
},
1079+
},
1080+
],
1081+
};
1082+
1083+
new JSONAPIDeserializer().deserialize(dataSet).then(function(json) {
1084+
expect(json).to.be.an('object').with.keys('id', 'owner', 'publisher');
1085+
expect(json.owner).to.exist;
1086+
expect(json.publisher).to.exist;
1087+
expect(json.owner).to.be.eql(json.publisher);
1088+
1089+
done(null, json);
1090+
});
1091+
});
1092+
});
10491093
});
10501094

10511095
describe('without callback', function () {
@@ -1133,7 +1177,12 @@ describe('JSON API Deserializer', function () {
11331177
id: '54735722e16620ba1eee36af',
11341178
country: {
11351179
country: 'USA',
1136-
id: '54735722e16609ba1eee36af'
1180+
id: '54735722e16609ba1eee36af',
1181+
address: {
1182+
address_line1: '406 Madison Court',
1183+
zip_code: '49426',
1184+
id: '54735722e16620ba1eee36af',
1185+
}
11371186
}
11381187
});
11391188
done(null, json);

0 commit comments

Comments
 (0)