Skip to content

Commit b27783e

Browse files
authored
Fix preventing circular relationships. (#108)
1 parent 08c7e38 commit b27783e

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

lib/JSONAPISerializer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,14 @@ module.exports = class JSONAPISerializer {
510510
const deserializeIncludedRelationship = (relationshipData) => {
511511
const lineageCopy = [...lineage];
512512
// Prevent circular relationships
513-
const isCircular = lineageCopy.includes(
514-
`${relationshipData.type}-${relationshipData.id}`
515-
);
513+
const lineageKey = `${relationshipData.type}-${relationshipData.id}`;
514+
const isCircular = lineageCopy.includes(lineageKey);
516515

517516
if (isCircular) {
518-
return deserializeFunction(data);
517+
return deserializeFunction(relationshipData);
519518
}
520519

521-
lineageCopy.push(`${type}-${data.id}`);
520+
lineageCopy.push(lineageKey);
522521
return this.deserializeIncluded(
523522
relationshipData.type,
524523
relationshipData.id,

test/unit/JSONAPISerializer.test.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ describe('JSONAPISerializer', function() {
11711171
};
11721172

11731173
const serializedData = Serializer.serialize('article', data);
1174-
1174+
11751175
const people1 = serializedData.included.find((include => include.type === 'people' && include.id === '1'));
11761176
expect(people1).to.have.property('relationships');
11771177
expect(people1.relationships).to.have.property('image');
@@ -1574,13 +1574,13 @@ describe('JSONAPISerializer', function() {
15741574
profile: { type: 'profile' }
15751575
},
15761576
});
1577-
1577+
15781578
Serializer.register('user', {
15791579
relationships: {
15801580
profile: { type: 'profile' },
15811581
},
15821582
});
1583-
1583+
15841584
Serializer.register('profile', {
15851585
relationships: {
15861586
user: { type: 'user' },
@@ -1595,31 +1595,38 @@ describe('JSONAPISerializer', function() {
15951595
"title": "title"
15961596
},
15971597
"relationships": {
1598-
"author": { "data": { "type": "user", "id": "1" } },
1599-
"user": { "data": { "type": "user", "id": "1" } },
1600-
"profile": { "data": { "type": "profile", "id": "1" } }
1598+
"author": { "data": { "type": "user", "id": "author_id" } },
1599+
"user": { "data": { "type": "user", "id": "user_id" } },
1600+
"profile": { "data": { "type": "profile", "id": "profile_id" } }
16011601
}
16021602
},
16031603
"included": [
16041604
{
16051605
"type": "user",
1606-
"id": "1",
1606+
"id": "author_id",
16071607
"attributes": {
16081608
"email": "user@example.com"
16091609
},
16101610
"relationships": {
1611-
"profile": { "data": { "type": "profile", "id": "1" } }
1611+
"profile": { "data": { "type": "profile", "id": "profile_id" } }
16121612
}
16131613
},
1614+
{
1615+
"type": "user",
1616+
"id": "user_id",
1617+
"attributes": {
1618+
"email": "user@example.com"
1619+
},
1620+
},
16141621
{
16151622
"type": "profile",
1616-
"id": "1",
1623+
"id": "profile_id",
16171624
"attributes": {
16181625
"firstName": "first-name",
16191626
"lastName": "last-name"
16201627
},
16211628
"relationships": {
1622-
"user": { "data": { "type": "user", "id": "1" } }
1629+
"user": { "data": { "type": "user", "id": "author_id" } }
16231630
}
16241631
}
16251632
]
@@ -1628,8 +1635,10 @@ describe('JSONAPISerializer', function() {
16281635
const deserializedData = Serializer.deserialize('article', data);
16291636
expect(deserializedData).to.have.property('id');
16301637
expect(deserializedData.author).to.have.property('id');
1638+
expect(deserializedData.author.id).to.equal('author_id');
16311639
expect(deserializedData.author.profile).to.have.property('id');
1632-
expect(deserializedData.author.profile.user).to.equal('1');
1640+
expect(deserializedData.author.profile.id).to.equal('profile_id');
1641+
expect(deserializedData.author.profile.user).to.equal('author_id');
16331642
expect(deserializedData.user).to.have.property('id');
16341643
expect(deserializedData.profile).to.have.property('id');
16351644
done();
@@ -1644,13 +1653,13 @@ describe('JSONAPISerializer', function() {
16441653
profile: {type: 'profile'}
16451654
},
16461655
});
1647-
1656+
16481657
Serializer.register('user', {
16491658
relationships: {
16501659
profile: { type: 'profile' },
16511660
},
16521661
});
1653-
1662+
16541663
Serializer.register('profile', {
16551664
relationships: {
16561665
user: { type: 'user' },

0 commit comments

Comments
 (0)