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

Fix discriminator type field update for custom discriminator model name #12947

Merged
merged 1 commit into from Feb 3, 2023

Conversation

wassil
Copy link

@wassil wassil commented Jan 25, 2023

Summary

Mongoose requires unique model names, so to avoid conflicts when using discriminators, you use 3 parameter syntax: model.discriminator(modelName, schema, discriminatorKeyValue).
When updating a document using e.g. updateOne, you're allowed to change discriminator key value when you provide overwriteDiscriminatorKey.
But the code responsible for updating the discriminator type was relying on modelName being equal to discriminatorKeyValue, which may not be the case if you use custom model name for your discriminator.

Schema.discriminators are indexed by model name and the code was using update value for discriminatorKey.

Examples

I modified an existing test to make it fail, then fixed it.

I'm not sure if I should keep the original test as is and add a new one, but the difference is just 2 lines - the model names for discriminators.

@vkarpov15 vkarpov15 added this to the 6.9.1 milestone Feb 3, 2023
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks 👍

@vkarpov15 vkarpov15 merged commit bcd3117 into Automattic:master Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants