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

Cannot define discriminator options on a Schema #14448

Closed
2 tasks done
forivall opened this issue Mar 19, 2024 · 1 comment
Closed
2 tasks done

Cannot define discriminator options on a Schema #14448

forivall opened this issue Mar 19, 2024 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@forivall
Copy link

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.x.x, 8.x.x

Node.js version

20.x

MongoDB server version

7.2.2

Typescript version (if applicable)

No response

Description

While the options for a discriminator can be defined on a Model, SchemaSubdocument or SchemaDocumentArray, they cannot be defined on a general Schema.

Steps to Reproduce

const clickedEventSchema = new mongoose.Schema({ element: String }, { discriminatorKey: 'kind' });
eventSchema.discriminator('ClickedEvent', clickedEventSchema, { value: 'click' });
//                                                            ^ this will not be used
const Event = mongoose.model('Event', eventSchema);
const doc = await Event.create({ kind: 'clicked', element: '#hero' });
doc.element; // '#hero'
doc instanceof ClickedModel; // this should be `true`, this will currently be `false`

Expected Behavior

Potential fix: master...forivall:mongoose:schema-discriminator-options

@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

const mongoose = require('mongoose');

const eventSchema = new mongoose.Schema({
  name: String
});
const clickedEventSchema = new mongoose.Schema({ element: String }, { discriminatorKey: 'kind' });
eventSchema.discriminator('ClickedEvent', clickedEventSchema, { value: 'click' });
//                                                            ^ this will not be used
const Event = mongoose.model('Event', eventSchema);

const ClickedModel = mongoose.model('Clicked', clickedEventSchema);


async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const doc = await Event.create({ kind: 'clicked', element: '#hero' });
  doc.element; // '#hero'
  console.log(doc instanceof ClickedModel); // this should be `true`, this will currently be `false`
  console.log('done');
}

run();

@vkarpov15 vkarpov15 added this to the 7.6.11 milestone Mar 28, 2024
vkarpov15 added a commit that referenced this issue Apr 2, 2024
vkarpov15 added a commit that referenced this issue Apr 4, 2024
fix(schema): support setting discriminator options in 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
Development

No branches or pull requests

3 participants