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
Regression of embedded discriminators defined using Schema.prototype.discriminator() #14109
Comments
One workaround is to add the discriminators to the schema before using the schema to define a document array. For example, the following prints the correct result: 'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const baseNestedDiscriminated = new Schema({
type: { type: Number, required: true },
}, { discriminatorKey: "type" });
class BaseClass {
type = 1;
whoAmI() {
return "I am baseNestedDiscriminated";
}
}
baseNestedDiscriminated.loadClass(BaseClass);
const outerDiscriminatedSchema = new Schema({
type: { type: Number, required: true },
}, { discriminatorKey: "type" });
class NumberTyped extends BaseClass {
type = 3;
whoAmI() {
return "I am NumberTyped";
}
}
class StringTyped extends BaseClass {
type = 4;
whoAmI() {
return "I am StringTyped";
}
}
baseNestedDiscriminated.discriminator(1, new Schema({}).loadClass(NumberTyped));
baseNestedDiscriminated.discriminator("3", new Schema({}).loadClass(StringTyped));
const containsNestedSchema = new Schema({
nestedDiscriminatedTypes: { type: [baseNestedDiscriminated], required: true },
})
class ContainsNested {
nestedDiscriminatedTypes = [];
type = 1;
whoAmI() {
return "I am ContainsNested";
}
}
containsNestedSchema.loadClass(ContainsNested);
outerDiscriminatedSchema.discriminator(1, containsNestedSchema);
const secondModel = mongoose.model("containsNestedSchema", containsNestedSchema);
void async function main()
{
await mongoose.connect("mongodb://127.0.0.1:27017/example");
mongoose.connection.db.dropDatabase();
let instance = await secondModel.create({ type: 1, nestedDiscriminatedTypes: [{type: 1}, {type: "3"}] });
console.log(instance.whoAmI()); // "I am ContainsNested" - Works
instance.nestedDiscriminatedTypes.forEach(item => console.log(item.whoAmI()));
// I am NumberTyped
// I am StringTyped
await mongoose.disconnect();
}(); We will check to see if there's a way we can make the order in which you add |
I applied the workaround on my project and I can confirm it now works as expected. I do hope the last fix pans out, but with this workaround and the other recent fixes have greatly simplified our code around the use of discriminators. Many thanks for the prompt replies and fixes! |
…r()` to subdocuments after defining parent schema Fix #14109
…s on discriminators underneath subdocs
fix: allow adding discriminators using `Schema.prototype.discriminator()` to subdocuments after defining parent schema
Prerequisites
Mongoose version
7.6.5, 8.0.1
Node.js version
18.16
MongoDB server version
6.0.5
Typescript version (if applicable)
No response
Description
Regression of #13898.
I remember testing the fix at the time and that it worked (although other issues cropped up).
I went back and tested this on previous versions and I couldn't find one it works on (7.6.4, 7.6.5, 8.0.1) or when checking out commit 3042ac129 specifically.
Steps to Reproduce
Expected Behavior
Expected Output:
The text was updated successfully, but these errors were encountered: