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

Discriminator Method Modifies Schema Argument in Mongoose #14821

Closed
2 tasks done
AminPainter opened this issue Aug 21, 2024 · 3 comments
Closed
2 tasks done

Discriminator Method Modifies Schema Argument in Mongoose #14821

AminPainter opened this issue Aug 21, 2024 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@AminPainter
Copy link

AminPainter commented Aug 21, 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

7.2.2

Node.js version

20.12.2

MongoDB server version

7.0

Typescript version (if applicable)

No response

Description

When using Mongoose's discriminator method to create a new discriminator from a parent model, the method modifies the schema object passed as an argument. Specifically, it adds fields from the parent model's schema into the discriminator's schema, which is an unexpected and potentially invalid behavior.

Steps to Reproduce

const mongoose = require('mongoose');

// 1. Define a parent model User:

const userSchema = new mongoose.Schema({
  username: String,
  email: String,
});
const User = mongoose.model('User', userSchema);

// 2. Define a discriminator schema adminSchema:

const adminSchema = new mongoose.Schema({
  someOtherPropertyOfAdmin: String,
});

// 3. Create a discriminator:
const Admin = User.discriminator('Admin', adminSchema);

// 4. Observe that the adminSchema has been modified to include fields from the userSchema.
console.log(adminSchema.obj);
console.log(Admin.schema.obj);

image

Expected Behavior

Should the adminSchema remain unmodified after being passed to the discriminator method? We expect the fields from the userSchema to be combined with the adminSchema internally by Mongoose, without affecting the original schema object passed as an argument.

@Tushar-4781
Copy link

Tushar-4781 commented Aug 21, 2024

"Facing the same issue. The schema variable I pass to the discriminator method is being modified. While we can work around it by creating the discriminator like this:

 const adminSchemaObj = { 
  someOtherPropertyOfAdmin: String
};
const Admin = User.discriminator('Admin', new Schema(adminSchemaObj));

the question remains: is it valid for the discriminator method to modify the argument, especially when it's a variable we're using?"

@kushagra-goyal-14
Copy link

facing the same issue

@vkarpov15 vkarpov15 added this to the 8.5.5 milestone Aug 21, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 21, 2024
@vkarpov15 vkarpov15 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 Aug 28, 2024
@vkarpov15
Copy link
Collaborator

Fixed by #14833 specifically 9526e66

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

4 participants