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(update): handle runValidators when using $set on a doc array in discriminator schema #12571

Merged
merged 1 commit into from Oct 20, 2022
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
6 changes: 3 additions & 3 deletions lib/schema/documentarray.js
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
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