Skip to content

Commit

Permalink
Merge pull request #14287 from Automattic/vkarpov15/gh-14269
Browse files Browse the repository at this point in the history
fix(document): handle setting nested path to spread doc with extra properties
  • Loading branch information
vkarpov15 authored Jan 24, 2024
2 parents 59ea552 + 943d6b0 commit 2a7fdef
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 2a7fdef

Please sign in to comment.