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

schema.recompileSchema exception when subDocument discriminator is defined before the model is compiled #14527

Closed
2 tasks done
ZachLeviPixel opened this issue Apr 16, 2024 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ZachLeviPixel
Copy link
Contributor

ZachLeviPixel commented Apr 16, 2024

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.3.1

Node.js version

18.16

MongoDB server version

6.3.0

Typescript version (if applicable)

No response

Description

The following example throws "discriminator with name 'X' already exists" when the model of the top level schema is compiled after the discriminator definitions have been created on the subdocument schema.

If you create the model first, recompile seems to work as expected.
Important to note that on either case, the schema behaves as expected without recompiling.

Steps to Reproduce

const mongoose = require("mongoose");

const shopItemSchema = new mongoose.Schema({}, { discriminatorKey: "type" });
const shopSchema = new mongoose.Schema({
    items: { type: [shopItemSchema], required: true },
});

const shopItemSubType = new mongoose.Schema({});
// const shopModel = mongoose.model("shop", shopSchema); // If the shop model is created before the schema is discriminated, recompile will work
shopItemSchema.discriminator(2, shopItemSubType);
const shopModel = mongoose.model("shop", shopSchema);

shopModel.recompileSchema(); // Error: Discriminator with name "2" already exists

Expected Behavior

The schema should recompile without error.

@vkarpov15 vkarpov15 added this to the 8.3.3 milestone Apr 24, 2024
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Apr 24, 2024
@vkarpov15
Copy link
Collaborator

Fixed by 23869fb, I got my issue numbers mixed up

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

No branches or pull requests

2 participants