Skip to content

Commit 631ce31

Browse files
committed
fix: data with relationships and no attributes should be in included (#128)
closes #128
1 parent 138f6fc commit 631ce31

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: lib/JSONAPISerializer.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,10 @@ module.exports = class JSONAPISerializer {
848848
const identifier = `${type}-${serializedRelationship.id}`;
849849

850850
// Not include relationship object which only contains an id
851-
if (serializedIncluded.attributes && Object.keys(serializedIncluded.attributes).length) {
851+
if (
852+
(serializedIncluded.attributes && Object.keys(serializedIncluded.attributes).length) ||
853+
(serializedIncluded.relationships && Object.keys(serializedIncluded.relationships).length)
854+
) {
852855
// Merge relationships data if already included
853856
if (included.has(identifier)) {
854857
const alreadyIncluded = included.get(identifier);

Diff for: test/unit/JSONAPISerializer.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,31 @@ describe('JSONAPISerializer', function() {
439439
done();
440440
});
441441

442+
it('should return serialized relationship data and included for a relationship object with at least relationships', function(done) {
443+
const serializer = new JSONAPISerializer();
444+
serializer.register('authors', {
445+
relationships: {
446+
friends: {
447+
type: 'people'
448+
}
449+
}
450+
});
451+
serializer.register('people');
452+
453+
const included = new Map();
454+
const serializedRelationshipData = serializer.serializeRelationship('authors', 'default', {id: '1', friends: ['1', '2']}, included);
455+
expect(serializedRelationshipData).to.have.property('type').to.eql('authors');
456+
expect(serializedRelationshipData).to.have.property('id').to.eql('1');
457+
const includedValue = [...included.values()];
458+
expect(includedValue).to.have.lengthOf(1);
459+
expect(includedValue[0]).to.have.property('type').to.eql('authors');
460+
expect(includedValue[0]).to.have.property('id').to.eql('1');
461+
expect(includedValue[0]).to.have.property('relationships').to.have.property('friends').to.have.property('data').to.have.lengthOf(2);
462+
expect(includedValue[0].relationships.friends.data[0]).to.have.property('type').to.eql('people');
463+
expect(includedValue[0].relationships.friends.data[0]).to.have.property('id').to.eql('1');
464+
done();
465+
});
466+
442467
it('should return serialized relationship data and populated included with a custom schema', function(done) {
443468
const Serializer2 = new JSONAPISerializer();
444469
// Custom schema 'only-name' for authors resource

0 commit comments

Comments
 (0)