Skip to content

Commit

Permalink
fix(document): allow setters to modify this when overwriting single…
Browse files Browse the repository at this point in the history
… nested subdoc

Fix #7585
  • Loading branch information
vkarpov15 committed Mar 24, 2019
1 parent fb8b39a commit bb78de7
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,10 +1014,25 @@ Document.prototype.$set = function $set(path, val, type, options) {
// a single nested doc. That's to make sure we get the correct context.
// Otherwise we would double-call the setter, see gh-7196.
if (this.schema.singleNestedPaths[path] == null) {
const setterContext = constructing && this.$__.$options.priorDoc ?
this.$__.$options.priorDoc :
this;
val = schema.applySetters(val, setterContext, false, priorVal);
// Init the new subdoc to the previous values of the doc, so
// getters/setters see the correct current state. We pass the new subdoc
// instead of the old subdoc because otherwise side effects in setters
// wouldn't work, see gh-7585
if (constructing && this.$__.$options.priorDoc) {
const priorVal = Object.assign({}, this.$__.$options.priorDoc._doc);
delete priorVal[this.schema.options.discriminatorKey];
init(this, priorVal, this._doc);
}

val = schema.applySetters(val, this, false, priorVal);

if (constructing && this.$__.$options.priorDoc) {
// Clear init-ed paths afterwards, because those should be paths that
// were in the previous doc and should not be in the new one.
for (const path of Object.keys(this.$__.activePaths.states.init)) {
delete this._doc[path];
}
}
}

if (!didPopulate && this.$__.populated) {
Expand Down

0 comments on commit bb78de7

Please sign in to comment.