Skip to content

Commit f2b0aec

Browse files
jorgelfsushantdhiman
authored andcommitted
fix(model): don't alter original scopes when combining them (#10722)
1 parent 75b95dc commit f2b0aec

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

lib/model.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,12 @@ class Model {
811811
}
812812
});
813813
}
814+
// If we have a possible object/array to clone, we try it.
815+
// Otherwise, we return the original value when it's not undefined,
816+
// or the resulting object in that case.
817+
if (srcValue) {
818+
return Utils.cloneDeep(srcValue, true);
819+
}
814820
return srcValue === undefined ? objValue : srcValue;
815821
}
816822

lib/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,17 @@ function formatNamedParameters(sql, parameters, dialect) {
123123
}
124124
exports.formatNamedParameters = formatNamedParameters;
125125

126-
function cloneDeep(obj) {
126+
function cloneDeep(obj, onlyPlain) {
127127
obj = obj || {};
128128
return _.cloneDeepWith(obj, elem => {
129129
// Do not try to customize cloning of arrays or POJOs
130130
if (Array.isArray(elem) || _.isPlainObject(elem)) {
131131
return undefined;
132132
}
133133

134-
// Don't clone stuff that's an object, but not a plain one - fx example sequelize models and instances
135-
if (typeof elem === 'object') {
134+
// If we specified to clone only plain objects & arrays, we ignore everyhing else
135+
// In any case, don't clone stuff that's an object, but not a plain one - fx example sequelize models and instances
136+
if (onlyPlain || typeof elem === 'object') {
136137
return elem;
137138
}
138139

test/unit/model/scope.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
112112
it('should unite attributes with array', () => {
113113
expect(User.scope('aScope', 'defaultScope')._scope.attributes).to.deep.equal({ exclude: ['value', 'password'] });
114114
});
115+
116+
it('should not modify the original scopes when merging them', () => {
117+
expect(User.scope('defaultScope', 'aScope').options.defaultScope.attributes).to.deep.equal({ exclude: ['password'] });
118+
});
115119
});
116120

117121
it('defaultScope should be an empty object if not overridden', () => {

0 commit comments

Comments
 (0)