From 03b7031045aa46e1e974b1bee1ce4747aabbb6ca Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 26 Apr 2024 17:25:17 -0400 Subject: [PATCH 1/2] types(document): make document _id type default to unknown instead of any Fix #14520 --- types/document.d.ts | 2 +- types/types.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/document.d.ts b/types/document.d.ts index 2bb82e3c677..c0723e883bf 100644 --- a/types/document.d.ts +++ b/types/document.d.ts @@ -16,7 +16,7 @@ declare module 'mongoose' { * * TQueryHelpers - Object with any helpers that should be mixed into the Query type * * DocType - the type of the actual Document created */ - class Document { + class Document { constructor(doc?: any); /** This documents _id. */ diff --git a/types/types.d.ts b/types/types.d.ts index f63b1934907..08f90c6184c 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -83,7 +83,7 @@ declare module 'mongoose' { class ObjectId extends mongodb.ObjectId { } - class Subdocument extends Document { + class Subdocument extends Document { $isSingleNested: true; /** Returns the top level document of this sub-document. */ From 23869fb10cd3e81c1bb2b51878a206ea30bb935e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 27 Apr 2024 11:20:42 -0400 Subject: [PATCH 2/2] fix(model): make `recompileSchema()` overwrite existing document array discriminators Fix #14520 --- lib/schema/documentArray.js | 2 +- test/model.test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/schema/documentArray.js b/lib/schema/documentArray.js index 2af276500ff..d35c5dfbf10 100644 --- a/lib/schema/documentArray.js +++ b/lib/schema/documentArray.js @@ -195,7 +195,7 @@ SchemaDocumentArray.prototype.discriminator = function(name, schema, options) { schema = schema.clone(); } - schema = discriminator(this.casterConstructor, name, schema, tiedValue); + schema = discriminator(this.casterConstructor, name, schema, tiedValue, null, null, options?.overwriteExisting); const EmbeddedDocument = _createConstructor(schema, null, this.casterConstructor); EmbeddedDocument.baseCasterConstructor = this.casterConstructor; diff --git a/test/model.test.js b/test/model.test.js index 840b098a86a..9d35f207000 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -7463,6 +7463,23 @@ describe('Model', function() { assert.equal(instance.item.whoAmI(), 'I am Test2'); }); + it('overwrites existing discriminators when calling recompileSchema (gh-14527) (gh-14444)', async function() { + const shopItemSchema = new mongoose.Schema({}, { discriminatorKey: 'type' }); + const shopSchema = new mongoose.Schema({ + items: { type: [shopItemSchema], required: true } + }); + + const shopItemSubType = new mongoose.Schema({ prop: Number }); + shopItemSchema.discriminator(2, shopItemSubType); + const shopModel = db.model('shop', shopSchema); + + shopModel.recompileSchema(); + const doc = new shopModel({ + items: [{ type: 2, prop: 42 }] + }); + assert.equal(doc.items[0].prop, 42); + }); + it('inserts versionKey even if schema has `toObject.versionKey` set to false (gh-14344)', async function() { const schema = new mongoose.Schema( { name: String },