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

Reusing schema with discriminated field fails with "discriminator already exists" error #14162

Closed
2 tasks done
ZachLeviPixel opened this issue Dec 7, 2023 · 0 comments · Fixed by #14202
Closed
2 tasks done
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

7.6.7

Node.js version

18.16

MongoDB server version

6.0.5

Typescript version (if applicable)

No response

Description

I am not sure I caught the minimal scenario where this issue occurs, but it seems that reusing a schema object that contains a discriminated field causes an error. Defining an exact duplicate of the schema does not cause this error.

Steps to Reproduce

'use strict';

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

const discriminated = new Schema({
    type: { type: Number, required: true },
}, { discriminatorKey: "type" });

discriminated.discriminator(1, new Schema({}));
discriminated.discriminator(3, new Schema({}));

const containerSchema = new Schema({items: [discriminated]});
const containerModel = mongoose.model("container", containerSchema);
//const containerModel2 = mongoose.model("container2", new Schema({items: [discriminated]})); // Works
const containerModel2 = mongoose.model("container2", containerSchema); // Error: Discriminator with name "1" already exists
//const containerModel3 = mongoose.model("container3", new Schema({moreItems: [containerSchema]})); // Error: Discriminator with name "1" already exists

Expected Behavior

Code should run without errors.

@vkarpov15 vkarpov15 added this to the 7.6.8 milestone Dec 7, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Dec 7, 2023
@IslandRhythms IslandRhythms added 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 Dec 11, 2023
vkarpov15 added a commit that referenced this issue Dec 28, 2023
…s defined using Schema.prototype.discriminator

Fix #14162
vkarpov15 added a commit that referenced this issue Dec 30, 2023
fix(discriminator): handle reusing schema with embedded discriminators defined using Schema.prototype.discriminator
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
3 participants