Skip to content

Commit

Permalink
fix(document): dont leave nested key as undefined when setting nested…
Browse files Browse the repository at this point in the history
… key to empty object with minimize

Fix #8565
  • Loading branch information
vkarpov15 committed Feb 20, 2020
1 parent 1629bae commit f4dc5a9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,11 @@ Document.prototype.$set = function $set(path, val, type, options) {
keys = Object.keys(path);
const len = keys.length;

if (len === 0 && !this.schema.options.minimize) {
// `_skipMinimizeTopLevel` is because we may have deleted the top-level
// nested key to ensure key order.
const _skipMinimizeTopLevel = get(options, '_skipMinimizeTopLevel', false);
if (len === 0 && (!this.schema.options.minimize || _skipMinimizeTopLevel)) {
delete options._skipMinimizeTopLevel;
if (val) {
this.$set(val, {});
}
Expand Down Expand Up @@ -924,6 +928,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
this._doc[key] != null &&
Object.keys(this._doc[key]).length === 0) {
delete this._doc[key];
// Make sure we set `{}` back even if we minimize re: gh-8565
options = Object.assign({}, options, { _skipMinimizeTopLevel: true });
}

if (typeof path[key] === 'object' &&
Expand Down

0 comments on commit f4dc5a9

Please sign in to comment.