不要怂,就是干,撸起袖子干!

Commit fc942b08 by Maks Nemisj Committed by Sushant

fix(instance): toJSON should return cloned version of internal object (#8171)

1 parent d4b82124
...@@ -3995,9 +3995,11 @@ class Model { ...@@ -3995,9 +3995,11 @@ class Model {
* @return {object} * @return {object}
*/ */
toJSON() { toJSON() {
return this.get({ return Utils._.clone(
plain: true this.get({
}); plain: true
})
);
} }
/** /**
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), () => {
describe('toJSON', () => {
it('returns copy of json', () => {
const User = current.define('User', {
name: DataTypes.STRING
});
const user = User.build({ name: 'my-name' });
const json1 = user.toJSON();
expect(json1).to.have.property('name').and.be.equal('my-name');
// remove value from json and ensure it's not changed in the instance
delete json1.name;
const json2 = user.toJSON();
expect(json2).to.have.property('name').and.be.equal('my-name');
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!