Skip to content

Commit

Permalink
Merge pull request #15243 from Automattic/vkarpov15/gh-15218
Browse files Browse the repository at this point in the history
fix: infer discriminator key if set in `$set` with overwriteDiscriminatorKey
  • Loading branch information
vkarpov15 authored Feb 8, 2025
2 parents 6838d48 + 404b74b commit 1a2faa7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@
* fix: disallow using $where in match
* perf: cache results from getAllSubdocs() on saveOptions, only loop through known subdoc properties #15055 #15029
* fix(model+query): support overwriteDiscriminatorKey for bulkWrite updateOne and updateMany, allow inferring discriminator key from update #15046 #15040
=======
>>>>>>> 7.x

7.8.3 / 2024-11-26
==================
Expand Down
10 changes: 10 additions & 0 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
schema = schema.discriminators[discriminatorValue] ||
(byValue && byValue.schema) ||
schema;
} else if (schema != null &&
options.overwriteDiscriminatorKey &&
obj.$set != null &&
utils.hasUserDefinedProperty(obj.$set, schema.options.discriminatorKey) &&
schema.discriminators != null) {
const discriminatorValue = obj.$set[schema.options.discriminatorKey];
const byValue = getDiscriminatorByValue(context.model.discriminators, discriminatorValue);
schema = schema.discriminators[discriminatorValue] ||
(byValue && byValue.schema) ||
schema;
}

if (options.upsert) {
Expand Down
12 changes: 9 additions & 3 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4174,7 +4174,7 @@ describe('Model', function() {
assert.strictEqual(r2.testArray[0].nonexistentProp, undefined);
});

it('handles overwriteDiscriminatorKey (gh-15040)', async function() {
it('handles overwriteDiscriminatorKey (gh-15218) (gh-15040)', async function() {
const dSchema1 = new mongoose.Schema({
field1: String
});
Expand Down Expand Up @@ -4202,7 +4202,7 @@ describe('Model', function() {
assert.equal(r1.field1, 'field1');
assert.equal(r1.key, type1Key);

const field2 = 'field2';
let field2 = 'field2';
await TestModel.bulkWrite([{
updateOne: {
filter: { _id: r1._id },
Expand All @@ -4214,7 +4214,13 @@ describe('Model', function() {
}
}]);

const r2 = await TestModel.findById(r1._id);
let r2 = await TestModel.findById(r1._id);
assert.equal(r2.key, type2Key);
assert.equal(r2.field2, field2);

field2 = 'field2 updated again';
await TestModel.updateOne({ _id: r1._id }, { $set: { key: type2Key, field2 } }, { overwriteDiscriminatorKey: true });
r2 = await TestModel.findById(r1._id);
assert.equal(r2.key, type2Key);
assert.equal(r2.field2, field2);
});
Expand Down

0 comments on commit 1a2faa7

Please sign in to comment.