Skip to content

Commit 8b2a662

Browse files
committed
fix(document): avoid setting nested properties on top-level document when initing with strict: false
Fix #11526 Re: #11309
1 parent 40644db commit 8b2a662

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/document.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ function init(self, obj, doc, opts, prefix) {
801801
init(self, obj[i], doc[i], opts, path + '.');
802802
} else if (!schemaType) {
803803
doc[i] = obj[i];
804-
if (!strict) {
804+
if (!strict && !prefix) {
805805
self[i] = obj[i];
806806
}
807807
} else {

test/document.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -11132,10 +11132,26 @@ describe('document', function() {
1113211132

1113311133
const doc = await Test.findOne();
1113411134
doc.co.value = 123;
11135-
doc.co = doc.co; // < If this line is not present, there is no problem
11135+
doc.co = doc.co;
1113611136
await doc.save();
1113711137

1113811138
const res = await Test.findById(doc._id);
1113911139
assert.strictEqual(res.co.value, 123);
1114011140
});
11141+
11142+
it('avoids setting nested properties on top-level document when init-ing with strict: false (gh-11526) (gh-11309)', async function() {
11143+
const testSchema = Schema({ name: String }, { strict: false, strictQuery: false });
11144+
const Test = db.model('Test', testSchema);
11145+
11146+
const doc = new Test();
11147+
doc.init({
11148+
details: {
11149+
person: {
11150+
name: 'Baz'
11151+
}
11152+
}
11153+
});
11154+
11155+
assert.strictEqual(doc.name, void 0);
11156+
});
1114111157
});

0 commit comments

Comments
 (0)