Skip to content

Commit

Permalink
fix(document): create new doc when setting single nested, no more set…
Browse files Browse the repository at this point in the history
…() on copy of priorVal

Fix #5693
Re: #5363
  • Loading branch information
vkarpov15 committed Oct 8, 2017
1 parent f472f5e commit 140402c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
8 changes: 6 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ var idGetter = require('./plugins/idGetter');
* @api private
*/

function Document(obj, fields, skipId) {
function Document(obj, fields, skipId, options) {
this.$__ = new InternalCache;
this.$__.emitter = new EventEmitter();
this.isNew = true;
this.errors = undefined;
this.$__.$options = options || {};

var schema = this.schema;

Expand Down Expand Up @@ -757,7 +758,10 @@ Document.prototype.set = function(path, val, type, options) {
didPopulate = true;
}

val = schema.applySetters(val, this, false, priorVal);
var setterContext = constructing && this.$__.$options.priorDoc ?
this.$__.$options.priorDoc :
this;
val = schema.applySetters(val, setterContext, false, priorVal);

if (!didPopulate && this.$__.populated) {
delete this.$__.populated[path];
Expand Down
20 changes: 6 additions & 14 deletions lib/schema/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ Embedded.prototype = Object.create(SchemaType.prototype);
function _createConstructor(schema) {
var _embedded = function SingleNested(value, path, parent) {
var _this = this;
Subdocument.apply(this, arguments);

this.$parent = parent;
Subdocument.apply(this, arguments);

if (parent) {
parent.on('save', function() {
_this.emit('save', _this);
Expand Down Expand Up @@ -154,19 +156,9 @@ Embedded.prototype.cast = function(val, doc, init, priorVal) {
return new Constructor({}, doc ? doc.$__.selected : void 0, doc);
}

if (priorVal && priorVal.$isSingleNested) {
var _priorVal = priorVal.toObject({
minimize: true,
transform: false,
virtuals: false,
getters: false
});
subdoc = new Constructor(_priorVal, doc ? doc.$__.selected : void 0, doc);
} else {
subdoc = new Constructor(void 0, doc ? doc.$__.selected : void 0, doc);
}

subdoc.set(val, undefined, false);
return new Constructor(val, doc ? doc.$__.selected : void 0, doc, undefined, {
priorDoc: priorVal
});
}

return subdoc;
Expand Down
12 changes: 6 additions & 6 deletions lib/types/subdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = Subdocument;
* @api private
*/

function Subdocument(value, fields) {
function Subdocument(value, fields, parent, skipId, options) {
this.$isSingleNested = true;
Document.call(this, value, fields);
Document.call(this, value, fields, skipId, options);
}

Subdocument.prototype = Object.create(Document.prototype);
Expand Down Expand Up @@ -48,15 +48,15 @@ Subdocument.prototype.save = function(fn) {
};

Subdocument.prototype.$isValid = function(path) {
if (this.$parent) {
if (this.$parent && this.$basePath) {
return this.$parent.$isValid([this.$basePath, path].join('.'));
}
return Document.prototype.$isValid.call(this, path);
};

Subdocument.prototype.markModified = function(path) {
Document.prototype.markModified.call(this, path);
if (this.$parent) {
if (this.$parent && this.$basePath) {
if (this.$parent.isDirectModified(this.$basePath)) {
return;
}
Expand All @@ -66,7 +66,7 @@ Subdocument.prototype.markModified = function(path) {

Subdocument.prototype.$markValid = function(path) {
Document.prototype.$markValid.call(this, path);
if (this.$parent) {
if (this.$parent && this.$basePath) {
this.$parent.$markValid([this.$basePath, path].join('.'));
}
};
Expand All @@ -79,7 +79,7 @@ Subdocument.prototype.invalidate = function(path, err, val) {
Document.prototype.invalidate.call(this, path, err, val);
}

if (this.$parent) {
if (this.$parent && this.$basePath) {
this.$parent.invalidate([this.$basePath, path].join('.'), err, val);
} else if (err.kind === 'cast' || err.name === 'CastError') {
throw err;
Expand Down

0 comments on commit 140402c

Please sign in to comment.