Skip to content

Commit 3e63142

Browse files
committedMar 21, 2024
fix(document): avoid depopulating populated subdocs underneath document arrays when copying to another document
Fix #14118
1 parent 9ff5e42 commit 3e63142

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed
 

‎lib/document.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,11 @@ Document.prototype.$set = function $set(path, val, type, options) {
10871087
if (path.$__isNested) {
10881088
path = path.toObject();
10891089
} else {
1090-
path = path._doc;
1090+
// This ternary is to support gh-7898 (copying virtuals if same schema)
1091+
// while not breaking gh-10819, which for some reason breaks if we use toObject()
1092+
path = path.$__schema === this.$__schema
1093+
? applyVirtuals(path, { ...path._doc })
1094+
: path._doc;
10911095
}
10921096
}
10931097
if (path == null) {
@@ -4012,11 +4016,11 @@ function applyVirtuals(self, json, options, toObjectOptions) {
40124016
? toObjectOptions.aliases
40134017
: true;
40144018

4019+
options = options || {};
40154020
let virtualsToApply = null;
40164021
if (Array.isArray(options.virtuals)) {
40174022
virtualsToApply = new Set(options.virtuals);
4018-
}
4019-
else if (options.virtuals && options.virtuals.pathsToSkip) {
4023+
} else if (options.virtuals && options.virtuals.pathsToSkip) {
40204024
virtualsToApply = new Set(paths);
40214025
for (let i = 0; i < options.virtuals.pathsToSkip.length; i++) {
40224026
if (virtualsToApply.has(options.virtuals.pathsToSkip[i])) {
@@ -4029,7 +4033,6 @@ function applyVirtuals(self, json, options, toObjectOptions) {
40294033
return json;
40304034
}
40314035

4032-
options = options || {};
40334036
for (i = 0; i < numPaths; ++i) {
40344037
path = paths[i];
40354038

‎lib/model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5124,7 +5124,7 @@ function _assign(model, vals, mod, assignmentOpts) {
51245124
}
51255125
// flag each as result of population
51265126
if (!lean) {
5127-
val.$__.wasPopulated = val.$__.wasPopulated || true;
5127+
val.$__.wasPopulated = val.$__.wasPopulated || { value: _val };
51285128
}
51295129
}
51305130
}

‎lib/schema/documentarray.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,9 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
443443

444444
const Constructor = getConstructor(this.casterConstructor, rawArray[i]);
445445

446-
// Check if the document has a different schema (re gh-3701)
447-
if (rawArray[i].$__ != null && !(rawArray[i] instanceof Constructor)) {
448-
const spreadDoc = handleSpreadDoc(rawArray[i], true);
449-
if (rawArray[i] !== spreadDoc) {
450-
rawArray[i] = spreadDoc;
451-
} else {
452-
rawArray[i] = rawArray[i].toObject({
453-
transform: false,
454-
// Special case: if different model, but same schema, apply virtuals
455-
// re: gh-7898
456-
virtuals: rawArray[i].schema === Constructor.schema
457-
});
458-
}
446+
const spreadDoc = handleSpreadDoc(rawArray[i], true);
447+
if (rawArray[i] !== spreadDoc) {
448+
rawArray[i] = spreadDoc;
459449
}
460450

461451
if (rawArray[i] instanceof Subdocument) {

0 commit comments

Comments
 (0)
Please sign in to comment.