Skip to content

Commit

Permalink
fix(discriminator): handle modifying multiple nested paths underneath…
Browse files Browse the repository at this point in the history
… a discriminator

Fix #11428
Re: #6436
  • Loading branch information
vkarpov15 committed Feb 27, 2022
1 parent 601650a commit 49308ee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,6 @@ Document.prototype.$set = function $set(path, val, type, options) {
} else {
this.markModified(path);
}
cleanModifiedSubpaths(this, path, { skipDocArrays: true });
return this;
}
this.invalidate(path, new MongooseError.CastError('Object', val, path));
Expand Down
1 change: 1 addition & 0 deletions lib/helpers/document/cleanModifiedSubpaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function cleanModifiedSubpaths(doc, path, options) {
if (!doc) {
return deleted;
}

for (const modifiedPath of Object.keys(doc.$__.activePaths.states.modify)) {
if (skipDocArrays) {
const schemaType = doc.$__schema.path(modifiedPath);
Expand Down
40 changes: 40 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,7 @@ describe('model', function() {
assert.equal(res.c.gc.gc11, 'eleventy');
assert.equal(res.c.gc.gc12, 110);
});

it('Should allow reusing discriminators (gh-10931)', async function() {
const options = { discriminatorKey: 'kind', overwriteModels: true };
const eventSchema = new mongoose.Schema({ time: Date }, options);
Expand All @@ -1890,4 +1891,43 @@ describe('model', function() {
const reUseEvent = new reUse({ time: Date.now(), url: 'test.com' });
assert.ok(reUseEvent.url);
});

it('handles updating multiple properties nested underneath a discriminator (gh-11428)', async function() {
const StepSchema = new Schema({ url: String }, { discriminatorKey: 'type' });
const SheetReadOptionsSchema = new Schema({
location: {
id: String,
timeout: Number
}
});
const ClickSchema = new Schema(
[StepSchema, { sheetOptions: SheetReadOptionsSchema }],
{ discriminatorKey: 'type' }
);

const Step = db.model('Step', StepSchema);

Step.discriminator('click', ClickSchema);

const doc = await Step.create({
type: 'click',
url: 'https://google.com',
sheetOptions: {
location: {
id: 'currentCell',
timeout: 1000
}
}
});

doc.set({
'sheetOptions.location.id': 'inColumn',
'sheetOptions.location.timeout': 2000
});
await doc.save();

const updatedDoc = await Step.findOne({ type: 'click', _id: doc._id });
assert.equal(updatedDoc.sheetOptions.location.id, 'inColumn');
assert.equal(updatedDoc.sheetOptions.location.timeout, 2000);
});
});

0 comments on commit 49308ee

Please sign in to comment.