Skip to content

Commit dbd625c

Browse files
committed
fix(kitsu-core): don't serialise meta object as an attribute
1 parent 22a7bc9 commit dbd625c

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,8 @@ function isValid (isArray, type, payload, method) {
4646
function serialiseRelationOne (node, nodeType) {
4747
let relation = {}
4848
for (const prop of Object.keys(node)) {
49-
if (prop &&
50-
// Properties not being put in data.attributes
51-
![ 'id', 'type' ].includes(prop) &&
52-
// Exclude a valid JSON:API links object being put in data.attributes
53-
!(prop === 'links' && (node[prop].self || node[prop].related))
54-
) {
55-
relation = serialiseAttr(node[prop], prop, relation)
56-
} else relation[prop] = node[prop]
49+
if ([ 'id', 'type' ].includes(prop)) relation[prop] = node[prop]
50+
else relation = serialiseAttr(node[prop], prop, relation)
5751
}
5852
// Guess relationship type if not provided
5953
if (!relation.type) relation.type = nodeType
@@ -111,7 +105,8 @@ function serialiseRelation (node, nodeType, key, data) {
111105
*/
112106
function serialiseAttr (node, key, data) {
113107
if (!data.attributes) data.attributes = {}
114-
if (key === 'links' && (node.self || node.related)) data.links = node
108+
if (key === 'links' && (typeof node.self === 'string' || typeof node.related === 'string')) data.links = node
109+
else if (key === 'meta' && node.constructor === Object) data.meta = node
115110
else data.attributes[key] = node
116111
return data
117112
}

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,13 @@ describe('kitsu-core', () => {
463463
expect(input).toEqual({ data: resourceOutput })
464464
})
465465

466-
it('uses the new system', () => {
466+
it('serialises object and array relationships', () => {
467467
expect.assertions(1)
468468
const input = {
469469
id: '1',
470470
type: 'libraryEntries',
471471
links: { self: 'library-entries/1' },
472+
meta: { extra: true },
472473
ratingTwenty: 10,
473474
user: {
474475
links: {
@@ -504,6 +505,7 @@ describe('kitsu-core', () => {
504505
id: '1',
505506
type: 'libraryEntries',
506507
links: { self: 'library-entries/1' },
508+
meta: { extra: true },
507509
attributes: { ratingTwenty: 10 },
508510
relationships: {
509511
user: {
@@ -539,5 +541,45 @@ describe('kitsu-core', () => {
539541
}
540542
expect(serialise('libraryEntries', input)).toStrictEqual(output)
541543
})
544+
545+
it('keeps non-JSON:API links/meta properties in attributes', () => {
546+
expect.assertions(1)
547+
const input = {
548+
id: '1',
549+
type: 'libraryEntries',
550+
links: 'Not JSON:API link object',
551+
meta: 'Not JSON:API meta object',
552+
user: {
553+
data: {
554+
id: '1',
555+
links: 'Not JSON:API link object',
556+
meta: 'Not JSON:API meta object'
557+
}
558+
}
559+
}
560+
const output = {
561+
data: {
562+
id: '1',
563+
type: 'libraryEntries',
564+
attributes: {
565+
links: 'Not JSON:API link object',
566+
meta: 'Not JSON:API meta object'
567+
},
568+
relationships: {
569+
user: {
570+
data: {
571+
id: '1',
572+
type: 'user',
573+
attributes: {
574+
links: 'Not JSON:API link object',
575+
meta: 'Not JSON:API meta object'
576+
}
577+
}
578+
}
579+
}
580+
}
581+
}
582+
expect(serialise('libraryEntries', input)).toStrictEqual(output)
583+
})
542584
})
543585
})

0 commit comments

Comments
 (0)