Skip to content

Commit

Permalink
fix(kitsu-core): don't serialise meta object as an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian committed Jun 15, 2020
1 parent 22a7bc9 commit dbd625c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
13 changes: 4 additions & 9 deletions packages/kitsu-core/src/serialise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ function isValid (isArray, type, payload, method) {
function serialiseRelationOne (node, nodeType) {
let relation = {}
for (const prop of Object.keys(node)) {
if (prop &&
// Properties not being put in data.attributes
![ 'id', 'type' ].includes(prop) &&
// Exclude a valid JSON:API links object being put in data.attributes
!(prop === 'links' && (node[prop].self || node[prop].related))
) {
relation = serialiseAttr(node[prop], prop, relation)
} else relation[prop] = node[prop]
if ([ 'id', 'type' ].includes(prop)) relation[prop] = node[prop]
else relation = serialiseAttr(node[prop], prop, relation)
}
// Guess relationship type if not provided
if (!relation.type) relation.type = nodeType
Expand Down Expand Up @@ -111,7 +105,8 @@ function serialiseRelation (node, nodeType, key, data) {
*/
function serialiseAttr (node, key, data) {
if (!data.attributes) data.attributes = {}
if (key === 'links' && (node.self || node.related)) data.links = node
if (key === 'links' && (typeof node.self === 'string' || typeof node.related === 'string')) data.links = node
else if (key === 'meta' && node.constructor === Object) data.meta = node
else data.attributes[key] = node
return data
}
Expand Down
44 changes: 43 additions & 1 deletion packages/kitsu-core/src/serialise/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,13 @@ describe('kitsu-core', () => {
expect(input).toEqual({ data: resourceOutput })
})

it('uses the new system', () => {
it('serialises object and array relationships', () => {
expect.assertions(1)
const input = {
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' },
meta: { extra: true },
ratingTwenty: 10,
user: {
links: {
Expand Down Expand Up @@ -504,6 +505,7 @@ describe('kitsu-core', () => {
id: '1',
type: 'libraryEntries',
links: { self: 'library-entries/1' },
meta: { extra: true },
attributes: { ratingTwenty: 10 },
relationships: {
user: {
Expand Down Expand Up @@ -539,5 +541,45 @@ describe('kitsu-core', () => {
}
expect(serialise('libraryEntries', input)).toStrictEqual(output)
})

it('keeps non-JSON:API links/meta properties in attributes', () => {
expect.assertions(1)
const input = {
id: '1',
type: 'libraryEntries',
links: 'Not JSON:API link object',
meta: 'Not JSON:API meta object',
user: {
data: {
id: '1',
links: 'Not JSON:API link object',
meta: 'Not JSON:API meta object'
}
}
}
const output = {
data: {
id: '1',
type: 'libraryEntries',
attributes: {
links: 'Not JSON:API link object',
meta: 'Not JSON:API meta object'
},
relationships: {
user: {
data: {
id: '1',
type: 'user',
attributes: {
links: 'Not JSON:API link object',
meta: 'Not JSON:API meta object'
}
}
}
}
}
}
expect(serialise('libraryEntries', input)).toStrictEqual(output)
})
})
})

0 comments on commit dbd625c

Please sign in to comment.