You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
7.6.7, 8.0.3
Node.js version
18.16
MongoDB server version
6.0.5
Typescript version (if applicable)
No response
Description
A discriminated schema that recursively contains itself does not instantiate correct types further down the line as shown in the example. The issue occurs whether the recursive schema is defined before or after the base schema is completely defined.
Steps to Reproduce
'use strict';constmongoose=require('mongoose');const{ Schema }=mongoose;constbaseSchema=newSchema({type: {type: Number,required: true},},{discriminatorKey: "type"});classBase{type=1;whoAmI(){return"I am Base";}}baseSchema.loadClass(Base);classNumberTypedextendsBase{type=3;whoAmI(){return"I am NumberTyped";}}classStringTypedextendsBase{type=4;whoAmI(){return"I am StringTyped";}}constselfRefSchema=newSchema({self: {type: [baseSchema],required: true},})classSelfReferenceTypedextendsBase{selfReferences=[];type=5;whoAmI(){return"I am SelfReferenceTyped";}}selfRefSchema.loadClass(SelfReferenceTyped);baseSchema.discriminator(5,selfRefSchema);constnumberTypedSchema=newSchema({}).loadClass(NumberTyped);conststringTypedSchema=newSchema({}).loadClass(StringTyped);baseSchema.discriminator(1,numberTypedSchema);baseSchema.discriminator(3,stringTypedSchema);// Uncommenting these lines makes the script work as expected//selfRefSchema.path("self").discriminator(1, numberTypedSchema.loadClass(NumberTyped));//selfRefSchema.path("self").discriminator(3, stringTypedSchema.loadClass(StringTyped));constcontainerSchema=newSchema({items: [baseSchema]});constcontainerModel=mongoose.model('container',containerSchema);voidasyncfunctionmain(){awaitmongoose.connect("mongodb://127.0.0.1:27017/example");mongoose.connection.db.dropDatabase();letinstance=awaitcontainerModel.create({items: [{type: 1}]});// "I am NumberTyped" - Worksconsole.log(instance.items[0].whoAmI());instance=awaitcontainerModel.create({items: [{type: 3}]});// "I am StringTyped" Worksconsole.log(instance.items[0].whoAmI());instance=awaitcontainerModel.create({items: [{type: 5,self: [{type: 1},{type: 3}]}]});console.log(instance.items[0].whoAmI());// "I am SelfReferenceTyped" - Worksinstance.items[0].self.forEach(item=>console.log(item.whoAmI()));// Expected:// I am NumberTyped// I am StringTyped// Actual:// I am Base// I am Baseawaitmongoose.disconnect();}();
Expected Behavior
No response
The text was updated successfully, but these errors were encountered:
Prerequisites
Mongoose version
7.6.7, 8.0.3
Node.js version
18.16
MongoDB server version
6.0.5
Typescript version (if applicable)
No response
Description
A discriminated schema that recursively contains itself does not instantiate correct types further down the line as shown in the example. The issue occurs whether the recursive schema is defined before or after the base schema is completely defined.
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: