Skip to content

Commit 9a480b9

Browse files
committed
fix(kitsu-core): fix deserialisation of relationships from primary data
1 parent d5e20ac commit 9a480b9

2 files changed

Lines changed: 118 additions & 2 deletions

File tree

packages/kitsu-core/src/deserialise/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { linkRelationships } from '../linkRelationships'
1010
*/
1111
function deserialiseArray (array) {
1212
for (let value of array.data) {
13-
if (array.included) value = linkRelationships(value, array.included)
14-
if (value.relationships) value = linkRelationships(value)
13+
value = linkRelationships(value, [ ...array.data, ...(array.included || []) ])
1514
if (value.attributes) value = deattribute(value)
1615
array.data[array.data.indexOf(value)] = value
1716
}

packages/kitsu-core/src/deserialise/index.spec.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,5 +670,122 @@ describe('kitsu-core', () => {
670670
]
671671
})
672672
})
673+
674+
it('deserialises a relationship from the data key', () => {
675+
expect.assertions(1)
676+
677+
const input = deserialise({
678+
data: [
679+
{
680+
id: '1',
681+
type: 'anime',
682+
attributes: { name: 'A' },
683+
relationships: {
684+
prequel: {
685+
data: {
686+
type: 'anime',
687+
id: '42'
688+
}
689+
}
690+
}
691+
},
692+
{
693+
id: '2',
694+
type: 'anime',
695+
attributes: { name: 'B' },
696+
relationships: {
697+
prequel: {
698+
data: {
699+
type: 'anime',
700+
id: '1'
701+
}
702+
}
703+
}
704+
},
705+
{
706+
id: '3',
707+
type: 'anime',
708+
attributes: { name: 'C' },
709+
relationships: {
710+
prequel: {
711+
data: {
712+
type: 'anime',
713+
id: '4'
714+
}
715+
}
716+
}
717+
}
718+
],
719+
included: [
720+
{
721+
id: '4',
722+
type: 'anime',
723+
attributes: { name: 'D' },
724+
relationships: {
725+
prequel: {
726+
data: {
727+
type: 'anime',
728+
id: '42'
729+
}
730+
}
731+
}
732+
}
733+
]
734+
})
735+
736+
const output = {
737+
data: [
738+
{
739+
id: '1',
740+
type: 'anime',
741+
name: 'A',
742+
prequel: {
743+
data: {
744+
id: '42',
745+
type: 'anime'
746+
}
747+
}
748+
},
749+
{
750+
id: '2',
751+
type: 'anime',
752+
name: 'B',
753+
prequel: {
754+
data: {
755+
id: '1',
756+
type: 'anime',
757+
name: 'A',
758+
prequel: {
759+
data: {
760+
id: '42',
761+
type: 'anime'
762+
}
763+
}
764+
}
765+
}
766+
},
767+
{
768+
id: '3',
769+
type: 'anime',
770+
name: 'C',
771+
prequel: {
772+
data: {
773+
id: '4',
774+
type: 'anime',
775+
name: 'D',
776+
prequel: {
777+
data: {
778+
id: '42',
779+
type: 'anime'
780+
}
781+
}
782+
}
783+
}
784+
}
785+
]
786+
}
787+
788+
expect(input).toEqual(output)
789+
})
673790
})
674791
})

0 commit comments

Comments
 (0)