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): Throws error when updating a key name that match the discriminator key name on nested object #12534
(fix): Throws error when updating a key name that match the discriminator key name on nested object #12534
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though i dont know much about the internals of discriminators
Can a discriminator be set deeper and not only on root? if it's the case we should handle that |
@abarriel This is the question. I'll wait for more feedback and apply changes if needed. |
lib/helpers/query/castUpdate.js
Outdated
if (schema.discriminatorMapping != null && key === schema.options.discriminatorKey && !options.overwriteDiscriminatorKey) { | ||
if ( | ||
schema.discriminatorMapping != null && | ||
key === schema.options.discriminatorKey && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this (prefix ? prefix + '.' + key : key) === schema.options.discriminatorKey &&
Although we don't have test coverage for nested discriminator keys, it seems to work at first glance. For example, the following script works as expected.
'use strict';
const mongoose = require('mongoose');
mongoose.set('debug', true);
const { Schema } = mongoose;
run().catch(err => console.err(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
const schema = new Schema({ name: String, meta: { test: String } }, { discriminatorKey: 'meta.kind' });
const Test = mongoose.model('Test', schema);
const Disc = Test.discriminator('Disc', Schema({ test: String }));
const disc = new Disc({ name: 'test' });
await disc.save();
console.log(await Disc.findById(disc));
}
Given that, it would be great if this check didn't break on if discriminator key has a .
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I'll work on this
…ator key name on nested object closes Automattic#12517
bad82f8
to
a347470
Compare
Thanks 👍 |
closes #12517