You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
model.get() does not clone objects for JSON data-type #10052
@sushantdhiman That is an easy fix, and I will submit a pull request. I just wasn't sure what the intended behavior of .get() without a key was supposed to be. Do you believe only model.toJSON() should return a clone, but not model.get()? It seems in some cases .get returns a clone and some cases this.dataValues. This is why I submitted an issue instead of just a pull request with cloneDeep.
Here is an example of what I mean. Two cases where .get returns either a reference to this.dataValues OR a "clone":
constSequelize=require('sequelize')constsequelize=newSequelize({dialect: 'sqlite',storage: ':memory:',logging: false})constAlbum=sequelize.define('Album',{name: Sequelize.TEXT})constAlbumWithGetter=sequelize.define('AlbumWithGetter',{name: {type: Sequelize.TEXT,get(){// custom getter}}})Album.sync().then(()=>{returnAlbum.create({name: 'Slow Dance Showdown'})}).then(album=>{constdataCopy=album.get()dataCopy.name='Workout Jams'console.log('Album Name compare: '+(dataCopy.name===album.name))// true, because reference})AlbumWithGetter.sync().then(()=>{returnAlbumWithGetter.create({name: 'Slow Dance Showdown'})}).then(album=>{constdataCopy=album.get()dataCopy.name='Slow Sleepy Hits'console.log('AlbumWithGetter Name compare: '+(dataCopy.name===album.name))//false, because clonereturnAlbumWithGetter.sync()})
@sushantdhiman I wasn't talking about the exact line but the general location, you'd have to check if typeof this.dataValues[key] === 'object' before returning the data, avoid having to deep clone the entire object.
Do you believe only model.toJSON() should return a clone, but not model.get()? It seems in some cases .get returns a clone and some cases this.dataValues. This is why I submitted an issue instead of just a pull request with cloneDeep.
I personally believe they both (and other cases) all should return a deep cloned value. We never want mutations from get() or toJSON() to affect our next save or update call.
As for its implications I am not so sure
There is a clone option for get, but I don't see it used any where
Performance impact, I see internal codebase is using mostly key based case which should not be affected. This is just a concern, any incorrect behaviour should be correct regardless of performance impact.
This code is so old I am not sure, @justinkalland I say let's try a PR which iron out these cases so we return deep cloned value for all cases. As @SimonSchick suggested we can use selective cases to avoid cloning whole objects
I agree @sushantdhiman ,get() should always returned a deep clone value, and toJSON() should just alias get() with no key (as it is documented as doing). But I suspect that the inconsistent behavior of get() may be relied upon in sequelize.
I worked on a fix for this but changing get() gets complicated. It has a default option options.plain=false and so by default it returns references to included Model instances (rather than "clones" of their data). This behavior is in use many places. If this was a new library I would argue to remove options.plain and always return cloned. But with the age of Sequelize perhaps just changing the toJSON to cloneDeep would be an acceptable solution.
Activity
sushantdhiman commentedon Oct 21, 2018
We need to
cloneDeepsequelize/lib/model.js
Line 4190 in be958ac
SimonSchick commentedon Oct 21, 2018
@sushantdhiman shouldn't it perform the deep clone here instead?
sequelize/lib/model.js
Line 3308 in be958ac
clonein thetoJSONcall too.ALiangLiang commentedon Oct 21, 2018
It's only deep clone
Arraydata. But @justinkalland want to deep clone wholeObjectJSON.justinkalland commentedon Oct 21, 2018
@sushantdhiman That is an easy fix, and I will submit a pull request. I just wasn't sure what the intended behavior of
.get()without a key was supposed to be. Do you believe onlymodel.toJSON()should return a clone, but notmodel.get()? It seems in some cases.getreturns a clone and some casesthis.dataValues. This is why I submitted an issue instead of just a pull request withcloneDeep.Here is an example of what I mean. Two cases where
.getreturns either a reference tothis.dataValuesOR a "clone":SimonSchick commentedon Oct 21, 2018
@sushantdhiman I wasn't talking about the exact line but the general location, you'd have to check if
typeof this.dataValues[key] === 'object'before returning the data, avoid having to deep clone the entire object.sushantdhiman commentedon Oct 21, 2018
I personally believe they both (and other cases) all should return a deep cloned value. We never want mutations from
get()ortoJSON()to affect our next save or update call.As for its implications I am not so sure
get, but I don't see it used any whereThis code is so old I am not sure, @justinkalland I say let's try a PR which iron out these cases so we return deep cloned value for all cases. As @SimonSchick suggested we can use selective cases to avoid cloning whole objects
justinkalland commentedon Oct 21, 2018
I agree @sushantdhiman ,
get()should always returned a deep clone value, andtoJSON()should just aliasget()with no key (as it is documented as doing). But I suspect that the inconsistent behavior ofget()may be relied upon in sequelize.justinkalland commentedon Oct 21, 2018
I worked on a fix for this but changing
get()gets complicated. It has a default optionoptions.plain=falseand so by default it returns references to included Model instances (rather than "clones" of their data). This behavior is in use many places. If this was a new library I would argue to removeoptions.plainand always return cloned. But with the age of Sequelize perhaps just changing the toJSON to cloneDeep would be an acceptable solution.sushantdhiman commentedon Oct 22, 2018
@justinkalland Yeah deep clone for toJSON is good enough