Skip to content

Commit 30315ce

Browse files
committed
fix(document): avoid treating nested projection as inclusive when applying defaults
Fix #14115
1 parent 6760c54 commit 30315ce

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

lib/helpers/document/applyDefaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const isNestedProjection = require('../projection/isNestedProjection');
4+
35
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip) {
46
const paths = Object.keys(doc.$__schema.paths);
57
const plen = paths.length;
@@ -32,7 +34,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
3234
}
3335
} else if (exclude === false && fields && !included) {
3436
const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
35-
if (curPath in fields || (j === len - 1 && hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
37+
if ((curPath in fields && !isNestedProjection(fields[curPath])) || (j === len - 1 && hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
3638
included = true;
3739
} else if (hasIncludedChildren != null && !hasIncludedChildren[curPath]) {
3840
break;

lib/helpers/projection/hasIncludedChildren.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = function hasIncludedChildren(fields) {
2121
const keys = Object.keys(fields);
2222

2323
for (const key of keys) {
24+
2425
if (key.indexOf('.') === -1) {
2526
hasIncludedChildren[key] = 1;
2627
continue;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = function isNestedProjection(val) {
4+
if (val == null || typeof val !== 'object') {
5+
return false;
6+
}
7+
return val.$slice == null && val.$elemMatch == null && val.$meta == null && val.$ == null;
8+
};

0 commit comments

Comments
 (0)