Skip to content

Commit

Permalink
fix(document): don't overwrite defaults with undefined keys
Browse files Browse the repository at this point in the history
Fix #3981
  • Loading branch information
vkarpov15 committed Mar 24, 2016
1 parent 24bca87 commit de72d9b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ Document.prototype.set = function(path, val, type, options) {
pathtype = this.schema.pathType(pathName);

if (path[key] !== null
&& path[key] !== undefined
&& path[key] !== void 0
// need to know if plain object - no Buffer, ObjectId, ref, etc
&& utils.isObject(path[key])
&& (!path[key].constructor || utils.getFunctionName(path[key].constructor) === 'Object')
Expand All @@ -520,6 +520,12 @@ Document.prototype.set = function(path, val, type, options) {
this.schema.paths[pathName].options.ref)) {
this.set(path[key], prefix + key, constructing);
} else if (strict) {
// Don't overwrite defaults with undefined keys (gh-3981)
if (constructing && path[key] === void 0 &&
this.get(key) !== void 0) {
continue;
}

if (pathtype === 'real' || pathtype === 'virtual') {
// Check for setting single embedded schema to document (gh-3535)
if (this.schema.paths[pathName] &&
Expand All @@ -538,7 +544,7 @@ Document.prototype.set = function(path, val, type, options) {
throw new StrictModeError(key);
}
}
} else if (undefined !== path[key]) {
} else if (path[key] !== void 0) {
this.set(prefix + key, path[key], constructing);
}
}
Expand Down

0 comments on commit de72d9b

Please sign in to comment.