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

Recompiling schema does not reapply discriminators #14444

Closed
2 tasks done
ZachLeviPixel opened this issue Mar 18, 2024 · 1 comment · Fixed by #14500
Closed
2 tasks done

Recompiling schema does not reapply discriminators #14444

ZachLeviPixel opened this issue Mar 18, 2024 · 1 comment · Fixed by #14500
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ZachLeviPixel
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.2.2

Node.js version

18.16

MongoDB server version

6.3.0

Typescript version (if applicable)

No response

Description

I have a use case where I define a schema that uses a discriminated schema in a base package. I want to enable dependent packages to add their own implementations of the discriminated schema. However, it seems that once the model that uses the schema is compiled, adding further discriminated schemas are not applied.

Steps to Reproduce

'use strict';

const mongoose = require('mongoose');
const { Schema } = mongoose;

// Define discriminated schema
const decoratorSchema = new Schema({
    type: { type: String, required: true },
}, { discriminatorKey: "type" });

class Decorator {
    type;
    whoAmI() { return "I am BaseDeco"; }
}
decoratorSchema.loadClass(Decorator);

// Define discriminated class before model is compiled
class Deco1 extends Decorator { whoAmI() { return "I am Deco1"; }};
const deco1Schema = new Schema({}).loadClass(Deco1);
deco1Schema.loadClass(Deco1);
decoratorSchema.discriminator("Deco1", deco1Schema);

// Define model that uses discriminated schema
const shopSchema = new Schema({
    item: { type: decoratorSchema, required: true }
});

class Shop {};
shopSchema.loadClass(Shop);
const shopModel = mongoose.model("shop", shopSchema);

// Define another discriminated class after the model is compiled
class Deco2 extends Decorator { whoAmI() { return "I am Deco2"; }};
const deco2Schema = new Schema({}).loadClass(Deco2);
deco2Schema.loadClass(Deco2);
decoratorSchema.discriminator("Deco2", deco2Schema);

// Shouldn't recompiling the schema fix this?
shopModel.recompileSchema();

void async function main() 
{
    let instance = new shopModel({ item: {type: "Deco1"} }); // "I am Deco1" - Works
    console.log(instance.item.whoAmI());

    instance = new shopModel({ item: {type: "Deco2"} }); // "I am BaseDeco" - Should be Deco2
    console.log(instance.item.whoAmI());
}();

Expected Behavior

After recompiling a schema, it should also apply new discriminators of its fields.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 21, 2024
@IslandRhythms
Copy link
Collaborator

Labeling as bug, but could be feature request. Docs are unclear.

@vkarpov15 vkarpov15 modified the milestones: 8.2.4, 8.2.5 Mar 26, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.5, 7.6.11 Apr 2, 2024
@vkarpov15 vkarpov15 modified the milestones: 7.6.11, 8.3.1 Apr 4, 2024
vkarpov15 added a commit that referenced this issue Apr 7, 2024
fix(model): make `Model.recompileSchema()` also re-apply discriminators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants