Skip to content

Commit 04b3be8

Browse files
committed
fix: deserialize relationship when alternativeKey is set and relationship data is not in the included array
closes #116
1 parent b016d08 commit 04b3be8

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

lib/JSONAPISerializer.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,21 @@ module.exports = class JSONAPISerializer {
498498
);
499499
};
500500

501-
set(
502-
deserializedData,
503-
relationshipKey,
504-
Array.isArray(relationship.data)
505-
? relationship.data.map((d) => deserializeIncludedRelationship(d))
506-
: deserializeIncludedRelationship(relationship.data)
507-
);
501+
const deserializedIncludedRelationship = Array.isArray(relationship.data)
502+
? relationship.data.map((d) => deserializeIncludedRelationship(d))
503+
: deserializeIncludedRelationship(relationship.data);
504+
505+
// not set to deserializedData if alternativeKey is set and relationship data is not in the included array (value is the same as alternativeKey value)
506+
if (
507+
!(
508+
relationshipOptions &&
509+
relationshipOptions.alternativeKey &&
510+
deserializedIncludedRelationship.toString() ===
511+
get(deserializedData, relationshipOptions.alternativeKey).toString()
512+
)
513+
) {
514+
set(deserializedData, relationshipKey, deserializedIncludedRelationship);
515+
}
508516
}
509517
}
510518
}

test/unit/JSONAPISerializer.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,77 @@ describe('JSONAPISerializer', function() {
20812081
done();
20822082
});
20832083

2084+
it('should deserializing relationship when alternativeKey is set and relationship data is not in the included array', function (done) {
2085+
const Serializer = new JSONAPISerializer();
2086+
2087+
Serializer.register('article', {
2088+
id: 'id',
2089+
relationships: {
2090+
author: {
2091+
type: 'people',
2092+
alternativeKey: 'author_id',
2093+
},
2094+
},
2095+
});
2096+
2097+
Serializer.register('people', {
2098+
id: 'id',
2099+
relationships: {
2100+
role: {
2101+
alternativeKey: 'role_id',
2102+
type: 'role',
2103+
},
2104+
},
2105+
});
2106+
2107+
Serializer.register('role', {});
2108+
2109+
const data = {
2110+
data: {
2111+
type: 'article',
2112+
id: '1',
2113+
attributes: {
2114+
title: 'JSON API paints my bikeshed!',
2115+
body: 'The shortest article. Ever.',
2116+
created: '2015-05-22T14:56:29.000Z',
2117+
},
2118+
relationships: {
2119+
author: {
2120+
data: {
2121+
type: 'people',
2122+
id: '1',
2123+
},
2124+
},
2125+
},
2126+
},
2127+
included: [
2128+
{
2129+
type: 'people',
2130+
id: '1',
2131+
attributes: {
2132+
firstName: 'Kaley',
2133+
lastName: 'Maggio',
2134+
},
2135+
relationships: {
2136+
role: {
2137+
data: {
2138+
type: 'role',
2139+
id: '1',
2140+
},
2141+
},
2142+
},
2143+
},
2144+
],
2145+
};
2146+
2147+
const deserializedData = Serializer.deserialize('article', data);
2148+
expect(deserializedData).to.have.property('author_id').to.eql('1');
2149+
expect(deserializedData).to.have.property('author');
2150+
expect(deserializedData.author).to.have.property('role_id').to.eql('1');
2151+
expect(deserializedData.author).to.not.have.property('role');
2152+
done();
2153+
});
2154+
20842155
it('should deserialize with \'deserialize\' option as a function', function(done) {
20852156
const Serializer = new JSONAPISerializer();
20862157
Serializer.register('articles', {

0 commit comments

Comments
 (0)