Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): handle setting nested path to spread doc with extra properties #14287

Merged
merged 1 commit into from Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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