Fix discriminator type field update for custom discriminator model name #12947
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 provideoverwriteDiscriminatorKey
.But the code responsible for updating the discriminator type was relying on
modelName
being equal todiscriminatorKeyValue
, 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 fordiscriminatorKey
.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.