Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/schema/documentArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any, TQueryHelpers = any, DocType = any> {
class Document<T = unknown, TQueryHelpers = any, DocType = any> {
constructor(doc?: any);

/** This documents _id. */
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ declare module 'mongoose' {
class ObjectId extends mongodb.ObjectId {
}

class Subdocument<IdType = any, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
class Subdocument<IdType = unknown, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
$isSingleNested: true;

/** Returns the top level document of this sub-document. */
Expand Down