File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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} )
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments