Skip to content

Commit b058e42

Browse files
committed
fix(kitsu-core): correctly parse attributes.attributes (closes #137)
1 parent f4ced89 commit b058e42

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,21 @@ export function deattribute (data) {
3030
if (typeof data === 'object' && data !== null) {
3131
if (Array.isArray(data)) data.map(el => deattribute(el))
3232
else if (data.attributes && data.attributes.constructor === Object) {
33-
Object.keys(data.attributes).forEach(key => { data[key] = data.attributes[key] })
34-
delete data.attributes
33+
for (const key of Object.keys(data.attributes)) {
34+
// Hoist everything but attributes to parent to avoid issues with deleting
35+
// as can't delete data.attributes[key] as it will belete data[key] too
36+
if (!data.attributes.attributes) {
37+
data[key] = data.attributes[key]
38+
}
39+
}
40+
41+
// Replace attributes with attributes variable if it exists, else delete
42+
// attributes. Avoids deletion issue with object references
43+
if (data.attributes.attributes) {
44+
data.attributes = data.attributes.attributes
45+
} else {
46+
delete data.attributes
47+
}
3548
}
3649
}
3750
return data

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,16 @@ describe('kitsu-core', () => {
5353
expect(deattribute(map)).toEqual(map)
5454
expect(deattribute(weakMap)).toEqual(weakMap)
5555
})
56+
57+
it('allows attributes.attributes', () => {
58+
expect.assertions(1)
59+
expect(deattribute({
60+
attributes: {
61+
attributes: 'value'
62+
}
63+
})).toEqual({
64+
attributes: 'value'
65+
})
66+
})
5667
})
5768
})

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,38 @@ describe('kitsu-core', () => {
227227
}
228228
})
229229
})
230+
231+
it('accepts attribute.attribute in relationships', () => {
232+
expect.assertions(1)
233+
const data = {
234+
relationships: {
235+
author: {
236+
data: {
237+
id: '1',
238+
type: 'people'
239+
}
240+
}
241+
}
242+
}
243+
const included = [
244+
{
245+
id: '1',
246+
type: 'people',
247+
attributes: {
248+
attributes: 'Joe'
249+
}
250+
}
251+
]
252+
expect(linkRelationships(data, included))
253+
.toEqual({
254+
author: {
255+
data: {
256+
id: '1',
257+
attributes: 'Joe',
258+
type: 'people'
259+
}
260+
}
261+
})
262+
})
230263
})
231264
})

0 commit comments

Comments
 (0)