Skip to content

Commit cf50772

Browse files
authored
Merge pull request #15076 from Automattic/vkarpov15/gh-15051
fix: cast using overwritten embedded discriminator key when set
2 parents 76f92d2 + 7cbb295 commit cf50772

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/helpers/query/getEmbeddedDiscriminatorPath.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
2828
const updatedPathsByFilter = updatedPathsByArrayFilter(update);
2929

3030
for (let i = 0; i < parts.length; ++i) {
31-
const subpath = cleanPositionalOperators(parts.slice(0, i + 1).join('.'));
31+
const originalSubpath = parts.slice(0, i + 1).join('.');
32+
const subpath = cleanPositionalOperators(originalSubpath);
3233
schematype = schema.path(subpath);
3334
if (schematype == null) {
3435
continue;
@@ -56,6 +57,11 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
5657
discriminatorKey = filter[wrapperPath].$elemMatch[key];
5758
}
5859

60+
const discriminatorKeyUpdatePath = originalSubpath + '.' + key;
61+
if (discriminatorKeyUpdatePath in update) {
62+
discriminatorKey = update[discriminatorKeyUpdatePath];
63+
}
64+
5965
if (discriminatorValuePath in update) {
6066
discriminatorKey = update[discriminatorValuePath];
6167
}

test/model.updateOne.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,58 @@ describe('model: updateOne: ', function() {
30913091
assert.equal(doc.login.keys.length, 1);
30923092
assert.equal(doc.login.keys[0].id, 'test2');
30933093
});
3094+
3095+
it('casts using overwritten discriminator key schema (gh-15051)', async function() {
3096+
const embedDiscriminatorSchema = new mongoose.Schema({
3097+
field1: String
3098+
});
3099+
const embedDiscriminatorSchema2 = new mongoose.Schema({
3100+
field2: String
3101+
});
3102+
const embedSchema = new mongoose.Schema({
3103+
field: String,
3104+
key: String
3105+
}, { discriminatorKey: 'key' });
3106+
embedSchema.discriminator('Type1', embedDiscriminatorSchema);
3107+
embedSchema.discriminator('Type2', embedDiscriminatorSchema2);
3108+
3109+
const testSchema = new mongoose.Schema({
3110+
testArray: [embedSchema]
3111+
});
3112+
3113+
const TestModel = db.model('Test', testSchema);
3114+
const test = new TestModel({
3115+
testArray: [{
3116+
key: 'Type1',
3117+
field: 'field',
3118+
field1: 'field1'
3119+
}]
3120+
});
3121+
await test.save();
3122+
3123+
const field2update = 'field2 update';
3124+
await TestModel.updateOne(
3125+
{ _id: test._id },
3126+
{
3127+
$set: {
3128+
'testArray.$[element].key': 'Type2',
3129+
'testArray.$[element].field2': field2update
3130+
}
3131+
},
3132+
{
3133+
arrayFilters: [
3134+
{
3135+
'element._id': test.testArray[0]._id
3136+
}
3137+
],
3138+
overwriteDiscriminatorKey: true
3139+
}
3140+
);
3141+
3142+
const r2 = await TestModel.findById(test._id);
3143+
assert.equal(r2.testArray[0].key, 'Type2');
3144+
assert.equal(r2.testArray[0].field2, field2update);
3145+
});
30943146
});
30953147

30963148
async function delay(ms) {

0 commit comments

Comments
 (0)