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): Throws error when updating a key name that match the discriminator key name on nested object #12534

Merged
merged 2 commits into from Oct 17, 2022

Conversation

lpizzinidev
Copy link
Contributor

closes #12517

Copy link
Collaborator

@hasezoey hasezoey left a 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

@abarriel
Copy link
Contributor

abarriel commented Oct 7, 2022

Can a discriminator be set deeper and not only on root? if it's the case we should handle that
discriminatorKey: 'obj.objkind',

@lpizzinidev
Copy link
Contributor Author

Can a discriminator be set deeper and not only on root?

@abarriel This is the question.
I've been conservative and assumed that the discriminator may be set only on root properties.
The other approach, as you pointed out, would be to allow deeper discriminators, something like discriminatorKey: 'obj.objkind'.

I'll wait for more feedback and apply changes if needed.

if (schema.discriminatorMapping != null && key === schema.options.discriminatorKey && !options.overwriteDiscriminatorKey) {
if (
schema.discriminatorMapping != null &&
key === schema.options.discriminatorKey &&
Copy link
Collaborator

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 ..

Copy link
Contributor Author

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

@vkarpov15
Copy link
Collaborator

Thanks 👍

@vkarpov15 vkarpov15 merged commit e519505 into Automattic:master Oct 17, 2022
@hasezoey hasezoey added this to the 6.6.6 milestone Oct 18, 2022
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.

Throw when setting a key name that match the discriminator key name
4 participants