Skip to content

Commit fc92ef2

Browse files
authored
Merge relationships data if already included (#99)
closes #89
1 parent a386da9 commit fc92ef2

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

lib/JSONAPISerializer.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,30 @@ module.exports = class JSONAPISerializer {
819819
serializedRelationship.id = rData[rOptions.id].toString();
820820
// Not include relationship object which only contains an id
821821
if (!(Object.keys(rData).length === 1 && rData[rOptions.id])) {
822-
included.set(
823-
`${type}-${serializedRelationship.id}`,
824-
this.serializeResource(type, rData, rOptions, included, extraData, overrideSchemaOptions)
822+
const identifier = `${type}-${serializedRelationship.id}`;
823+
const serializedIncluded = this.serializeResource(
824+
type,
825+
rData,
826+
rOptions,
827+
included,
828+
extraData,
829+
overrideSchemaOptions
825830
);
831+
832+
// Merge relationships data if already included
833+
if (included.has(identifier)) {
834+
const alreadyIncluded = included.get(identifier);
835+
836+
if (serializedIncluded.relationships) {
837+
alreadyIncluded.relationships = {
838+
...alreadyIncluded.relationships,
839+
...serializedIncluded.relationships
840+
};
841+
included.set(identifier, alreadyIncluded);
842+
}
843+
} else {
844+
included.set(identifier, serializedIncluded);
845+
}
826846
}
827847
}
828848
return serializedRelationship;

test/unit/JSONAPISerializer.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,77 @@ describe('JSONAPISerializer', function() {
10861086
expect(serializedData.included[0].attributes).to.not.have.property('phone-number');
10871087
done();
10881088
});
1089+
1090+
it('should merge relationships data if already included', function(done) {
1091+
const Serializer = new JSONAPISerializer();
1092+
Serializer.register('article', {
1093+
relationships: {
1094+
author: {
1095+
type: 'people'
1096+
},
1097+
comments: {
1098+
type: 'comment'
1099+
}
1100+
}
1101+
});
1102+
Serializer.register('people', {
1103+
relationships: {
1104+
friends: {
1105+
type: 'people'
1106+
},
1107+
image: {
1108+
type: 'image'
1109+
}
1110+
}
1111+
});
1112+
Serializer.register('comment', {
1113+
relationships: {
1114+
author: {
1115+
type: 'people'
1116+
}
1117+
}
1118+
});
1119+
Serializer.register('image', {});
1120+
1121+
const data = {
1122+
id: '1',
1123+
title: 'JSON API paints my bikeshed!',
1124+
body: 'The shortest article. Ever.',
1125+
created: '2015-05-22T14:56:29.000Z',
1126+
author: {
1127+
id: '1',
1128+
firstName: 'Kaley',
1129+
lastName: 'Maggio',
1130+
friends: [{
1131+
id: '2',
1132+
firstName: 'Kaley2',
1133+
lastName: 'Maggio2',
1134+
}]
1135+
},
1136+
comments: [{
1137+
id: '1',
1138+
body: 'I Like !',
1139+
author: {
1140+
id: '1',
1141+
firstName: 'Kaley',
1142+
lastName: 'Maggio',
1143+
image: {
1144+
id: '1',
1145+
title: 'Beautiful picture',
1146+
}
1147+
}
1148+
}]
1149+
};
1150+
1151+
const serializedData = Serializer.serialize('article', data);
1152+
console.log(JSON.stringify(serializedData, null, 2));
1153+
1154+
const people1 = serializedData.included.find((include => include.type === 'people' && include.id === '1'));
1155+
expect(people1).to.have.property('relationships');
1156+
expect(people1.relationships).to.have.property('image');
1157+
expect(people1.relationships).to.have.property('friends');
1158+
done();
1159+
});
10891160
});
10901161

10911162
describe('serializeAsync', function() {

0 commit comments

Comments
 (0)