Skip to content

Commit 4ad6a9c

Browse files
committed
Fix relationships when two types start with the same string
1 parent 2c3b80f commit 4ad6a9c

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

lib/deserializer-utils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var _extend = require('lodash/extend');
66
var _transform = require('lodash/transform');
77
var Inflector = require('./inflector');
88

9+
const ancestrySeparator = ":";
10+
const idSeparator = ";";
11+
912
module.exports = function (jsonapi, data, opts) {
1013
var alreadyIncluded = [];
1114

@@ -36,7 +39,7 @@ module.exports = function (jsonapi, data, opts) {
3639
if (included) {
3740
// To prevent circular references, check if the record type
3841
// has already been processed in this thread
39-
if (ancestry.indexOf(included.type) > -1) {
42+
if (ancestry.indexOf(included.type + idSeparator) > -1) {
4043
return Promise
4144
.all([extractAttributes(included)])
4245
.then(function (results) {
@@ -47,7 +50,7 @@ module.exports = function (jsonapi, data, opts) {
4750
}
4851

4952
return Promise
50-
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id)])
53+
.all([extractAttributes(included), extractRelationships(included, ancestry + ancestrySeparator + included.type + idSeparator + included.id)])
5154
.then(function (results) {
5255
var attributes = results[0];
5356
var relationships = results[1];
@@ -146,7 +149,7 @@ module.exports = function (jsonapi, data, opts) {
146149

147150
this.perform = function () {
148151
return Promise
149-
.all([extractAttributes(data), extractRelationships(data, data.type + data.id)])
152+
.all([extractAttributes(data), extractRelationships(data, data.type + ancestrySeparator + data.id)])
150153
.then(function (results) {
151154
var attributes = results[0];
152155
var relationships = results[1];

test/deserializer.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,56 @@ describe('JSON API Deserializer', function () {
13941394
});
13951395
});
13961396
});
1397+
1398+
describe("With relationships using similar type names", function () {
1399+
it("should return all data without circular error", function (done) {
1400+
var dataSetSearch = {
1401+
data: {
1402+
type: "productsearch",
1403+
id: "47",
1404+
attributes: {
1405+
sku: "7TY55",
1406+
},
1407+
relationships: {
1408+
product: { data: { type: "products", id: "122" } },
1409+
},
1410+
},
1411+
included: [
1412+
{
1413+
type: "images",
1414+
id: "49",
1415+
attributes: {
1416+
mimeType: "image/jpeg",
1417+
},
1418+
relationships: {
1419+
product: { data: { type: "products", id: "122" } },
1420+
},
1421+
},
1422+
{
1423+
type: "products",
1424+
id: "122",
1425+
attributes: {
1426+
sku: "7TY55"
1427+
},
1428+
relationships: {
1429+
image: { data: { type: "images", id: "49" } },
1430+
},
1431+
},
1432+
],
1433+
};
1434+
1435+
// new JSONAPIDeserializer().deserialize(dataSetCart, function (err, json) {
1436+
new JSONAPIDeserializer().deserialize(dataSetSearch, function (err, json) {
1437+
expect(json).to.be.an('object').with.keys('id', 'sku', 'product');
1438+
expect(json.product).to.exist;
1439+
expect(json.product).to.be.an('object').with.keys('id', 'sku', 'image');
1440+
expect(json.product.image).to.exist;
1441+
expect(json.product.image).to.be.an('object').with.keys('id', 'mime-type', 'product');
1442+
1443+
done(null, json);
1444+
});
1445+
});
1446+
});
13971447
});
13981448

13991449
describe('without callback', function () {

0 commit comments

Comments
 (0)