Skip to content

Commit

Permalink
fix(document): handle setting nested path to spread doc with extra pr…
Browse files Browse the repository at this point in the history
…operties

Fix #14269
  • Loading branch information
vkarpov15 committed Jan 24, 2024
1 parent 880bb2c commit 943d6b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/document.js
Expand Up @@ -1162,7 +1162,7 @@ Document.prototype.$set = function $set(path, val, type, options) {

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

// if this doc is being constructed we should not trigger getters
const priorVal = (() => {
Expand Down
37 changes: 37 additions & 0 deletions test/document.test.js
Expand Up @@ -12916,6 +12916,43 @@ describe('document', function() {
doc.set({ nested: void 0 });
assert.strictEqual(doc.toObject().nested, void 0);
});

it('handles setting nested path to spread doc with extra properties (gh-14269)', async function() {
const addressSchema = new mongoose.Schema(
{
street: String,
city: String,
state: String,
zip: Number
},
{ _id: false }
);
const personSchema = new mongoose.Schema({
name: String,
age: Number,
address: addressSchema
});

const personModel = db.model('Person', personSchema);
const person = new personModel({
name: 'John',
age: 42,
address: {
street: '123 Fake St',
city: 'Springfield',
state: 'IL',
zip: 12345
}
});

await person.save();

person.address = {
...person.address,
zip: 54321
};
assert.equal(person.address.zip, 54321);
});
});

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

0 comments on commit 943d6b0

Please sign in to comment.