Skip to content

Commit 943d6b0

Browse files
committed
fix(document): handle setting nested path to spread doc with extra properties
Fix #14269
1 parent 880bb2c commit 943d6b0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
11621162

11631163
// Assume this is a Mongoose document that was copied into a POJO using
11641164
// `Object.assign()` or `{...doc}`
1165-
val = handleSpreadDoc(val);
1165+
val = handleSpreadDoc(val, true);
11661166

11671167
// if this doc is being constructed we should not trigger getters
11681168
const priorVal = (() => {

test/document.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12916,6 +12916,43 @@ describe('document', function() {
1291612916
doc.set({ nested: void 0 });
1291712917
assert.strictEqual(doc.toObject().nested, void 0);
1291812918
});
12919+
12920+
it('handles setting nested path to spread doc with extra properties (gh-14269)', async function() {
12921+
const addressSchema = new mongoose.Schema(
12922+
{
12923+
street: String,
12924+
city: String,
12925+
state: String,
12926+
zip: Number
12927+
},
12928+
{ _id: false }
12929+
);
12930+
const personSchema = new mongoose.Schema({
12931+
name: String,
12932+
age: Number,
12933+
address: addressSchema
12934+
});
12935+
12936+
const personModel = db.model('Person', personSchema);
12937+
const person = new personModel({
12938+
name: 'John',
12939+
age: 42,
12940+
address: {
12941+
street: '123 Fake St',
12942+
city: 'Springfield',
12943+
state: 'IL',
12944+
zip: 12345
12945+
}
12946+
});
12947+
12948+
await person.save();
12949+
12950+
person.address = {
12951+
...person.address,
12952+
zip: 54321
12953+
};
12954+
assert.equal(person.address.zip, 54321);
12955+
});
1291912956
});
1292012957

1292112958
describe('Check if instance function that is supplied in schema option is availabe', function() {

0 commit comments

Comments
 (0)