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

Recursive embedded discriminators losing data on 5.10.3+ #9600

Closed
qqilihq opened this issue Nov 30, 2020 · 2 comments
Closed

Recursive embedded discriminators losing data on 5.10.3+ #9600

qqilihq opened this issue Nov 30, 2020 · 2 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@qqilihq
Copy link

qqilihq commented Nov 30, 2020

Do you want to request a feature or report a bug?
bug

What is the current behavior?
We have a schema with “Recursive embedded discriminators in arrays” (implemented following these instructions).

When parsing a given, nested object structure, Mongoose 5.10.3+ (up until 5.10.18) loses parts of the object tree. Mongoose 5.10.2 and below work fine.

If the current behavior is a bug, please provide the steps to reproduce.

import mongoose from 'mongoose';

beforeAll(async () => mongoose.connect(process.env.MONGO_URL as string));

afterAll(async () => mongoose.disconnect());

it('nested schema', () => {
  const contentSchema = new mongoose.Schema({}, { discriminatorKey: 'type' });
  const nestedSchema = new mongoose.Schema({
    body: {
      children: [contentSchema],
    },
  });
  const childrenArraySchema = nestedSchema.path('body.children') as mongoose.Schema.Types.DocumentArray;
  childrenArraySchema.discriminator(
    'container',
    new mongoose.Schema({
      body: { children: childrenArraySchema },
    })
  );
  const Nested = mongoose.model<mongoose.Document>('nested', nestedSchema);

  const nestedDocument = new Nested({
    body: {
      children: [
        { type: 'container', body: { children: [] } },
        {
          type: 'container',
          body: {
            children: [
              {
                type: 'container',
                body: {
                  children: [{ type: 'container', body: { children: [] } }],
                },
              },
            ],
          },
        },
      ],
    },
  });
  // ok with 5.10.2
  // failes with 5.10.3+
  expect(nestedDocument.toJSON()).toEqual({
    _id: expect.toBeObject(),
    body: {
      children: [
        {
          _id: expect.toBeObject(),
          type: 'container',
          body: { children: [] },
        },
        {
          _id: expect.toBeObject(),
          type: 'container',
          body: {
            children: [
              {
                _id: expect.toBeObject(),
                type: 'container',
                // this gets lost:
                body: {
                  children: [
                    {
                      _id: expect.toBeObject(),
                      type: 'container',
                      body: {
                        children: [],
                      },
                    },
                  ],
                },
              },
            ],
          },
        },
      ],
    },
  });
});

What is the expected behavior?
Test should pass.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • NodeJS 12
  • Mongoose 5.10.3 … 5.10.18
  • MongoDB irrelevant, issue happens during parsing
@vkarpov15 vkarpov15 added this to the 5.11.1 milestone Nov 30, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Nov 30, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.11.1, 5.11.2, 5.11.3 Dec 1, 2020
@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 Dec 2, 2020
@vkarpov15
Copy link
Collaborator

Looks like this was an unintended consequence of #9370. We'll fix this for v5.11.3.

However, your example doesn't quite line up exactly with the one in the discriminator docs. Our tests are passing for that example, the difference is that you're relying on adding the same document array schema type childrenArraySchema to itself via a discriminator. The example in the docs instead relies on adding a schema (not a schema type) subEventSchema to itself.

I'm not quite sure how to get your example to line up with how the docs does it - recursion is confusing. But your approach should work going forward.

@qqilihq
Copy link
Author

qqilihq commented Dec 3, 2020

Great, looking forward! Thank you very much!

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

2 participants