Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): avoid saving applied defaults if path is deselected #12506

Merged
merged 3 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/helpers/projection/isPathExcluded.js
Expand Up @@ -12,6 +12,10 @@ const isDefiningProjection = require('./isDefiningProjection');
*/

module.exports = function isPathExcluded(projection, path) {
if (projection == null) {
return false;
}

if (path === '_id') {
return projection._id === 0;
}
Expand Down
20 changes: 20 additions & 0 deletions lib/model.js
Expand Up @@ -51,11 +51,13 @@ const {
getRelatedDBIndexes,
getRelatedSchemaIndexes
} = require('./helpers/indexes/getRelatedIndexes');
const isPathExcluded = require('./helpers/projection/isPathExcluded');
const decorateDiscriminatorIndexOptions = require('./helpers/indexes/decorateDiscriminatorIndexOptions');
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
const leanPopulateMap = require('./helpers/populate/leanPopulateMap');
const modifiedPaths = require('./helpers/update/modifiedPaths');
const parallelLimit = require('./helpers/parallelLimit');
const parentPaths = require('./helpers/path/parentPaths');
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline');
const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths');
const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
Expand Down Expand Up @@ -760,6 +762,19 @@ Model.prototype.$__delta = function() {
}
}

// If this path is set to default, and either this path or one of
// its parents is excluded, don't treat this path as dirty.
if (this.$isDefault(data.path) && this.$__.selected) {
if (data.path.indexOf('.') === -1 && isPathExcluded(this.$__.selected, data.path)) {
continue;
}

const pathsToCheck = parentPaths(data.path);
if (pathsToCheck.find(path => isPathExcluded(this.$__.isSelected, path))) {
continue;
}
}

if (divergent.length) continue;
if (value === undefined) {
operand(this, where, delta, data, 1, '$unset');
Expand Down Expand Up @@ -798,6 +813,11 @@ Model.prototype.$__delta = function() {
if (this.$__.version) {
this.$__version(where, delta);
}

if (Object.keys(delta).length === 0) {
return [where, null];
}

return [where, delta];
};

Expand Down