Skip to content

Commit

Permalink
fix(document): make toObject() use child schema flattenMaps optio…
Browse files Browse the repository at this point in the history
…n by default

Fix #9995
  • Loading branch information
vkarpov15 committed Mar 18, 2021
1 parent 5d96508 commit 9176d30
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3202,10 +3202,6 @@ Document.prototype.$toObject = function(options, json) {
options = utils.isPOJO(options) ? clone(options) : {};
options._calledWithOptions = options._calledWithOptions || clone(options);

if (!('flattenMaps' in options)) {
options.flattenMaps = defaultOptions.flattenMaps;
}

let _minimize;
if (options._calledWithOptions.minimize != null) {
_minimize = options.minimize;
Expand All @@ -3215,14 +3211,24 @@ Document.prototype.$toObject = function(options, json) {
_minimize = schemaOptions.minimize;
}

let flattenMaps;
if (options._calledWithOptions.flattenMaps != null) {
flattenMaps = options.flattenMaps;
} else if (defaultOptions.flattenMaps != null) {
flattenMaps = defaultOptions.flattenMaps;
} else {
flattenMaps = schemaOptions.flattenMaps;
}

// The original options that will be passed to `clone()`. Important because
// `clone()` will recursively call `$toObject()` on embedded docs, so we
// need the original options the user passed in, plus `_isNested` and
// `_parentOptions` for checking whether we need to depopulate.
const cloneOptions = Object.assign(utils.clone(options), {
_isNested: true,
json: json,
minimize: _minimize
minimize: _minimize,
flattenMaps: flattenMaps
});

if (utils.hasUserDefinedProperty(options, 'getters')) {
Expand Down

0 comments on commit 9176d30

Please sign in to comment.