Skip to content

Commit

Permalink
Merge pull request #12571 from Automattic/vkarpov15/gh-12518
Browse files Browse the repository at this point in the history
fix(update): handle runValidators when using `$set` on a doc array in discriminator schema
  • Loading branch information
vkarpov15 authored Oct 20, 2022
2 parents 9133393 + 2b8434d commit 7fb5114
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ function _createConstructor(schema, options, baseClass) {
Subdocument || (Subdocument = require('../types/ArraySubdocument'));

// compile an embedded document for this schema
function EmbeddedDocument(_value, parentArray) {
function EmbeddedDocument() {
Subdocument.apply(this, arguments);
if (parentArray == null || parentArray.getArrayParent() == null) {
if (this.__parentArray == null || this.__parentArray.getArrayParent() == null) {
return;
}
this.$session(parentArray.getArrayParent().$session());
this.$session(this.__parentArray.getArrayParent().$session());
}

schema._preCompile();
Expand Down
31 changes: 31 additions & 0 deletions test/model.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,37 @@ describe('model: update:', function() {
catch(done);
});

it('handles $set on document array in discriminator with runValidators (gh-12518)', async function() {
const options = { discriminatorKey: 'kind', runValidators: true };

const countrySchema = new mongoose.Schema({ title: String }, options);
const areasSubSchema = new mongoose.Schema({ country: [countrySchema] }, options);
const WorldSchema = new mongoose.Schema({ areas: areasSubSchema }, options);

const World = db.model(
'World',
new mongoose.Schema({ title: String }, options)
);
const Earth = World.discriminator('Earth', WorldSchema);

const data = {
areas: {
country: [
{
title: 'titlec'
}
]
}
};
await Earth.updateOne(
{ _id: mongoose.Types.ObjectId() },
data,
{
runValidators: true
}
);
});

it('single nested schema with geo (gh-4465)', function(done) {
const addressSchema = new Schema({
geo: { type: [Number], index: '2dsphere' }
Expand Down

0 comments on commit 7fb5114

Please sign in to comment.