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 nested discriminators #14245

Closed
2 tasks done
ZachLeviPixel opened this issue Jan 9, 2024 · 0 comments
Closed
2 tasks done

Recursive nested discriminators #14245

ZachLeviPixel opened this issue Jan 9, 2024 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ZachLeviPixel
Copy link
Contributor

ZachLeviPixel commented Jan 9, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.6.7, 8.0.3

Node.js version

18.16

MongoDB server version

6.0.5

Typescript version (if applicable)

No response

Description

A discriminated schema that recursively contains itself does not instantiate correct types further down the line as shown in the example. The issue occurs whether the recursive schema is defined before or after the base schema is completely defined.

Steps to Reproduce

'use strict';

const mongoose = require('mongoose');
const { Schema } = mongoose;

const baseSchema = new Schema({
    type: { type: Number, required: true },
}, { discriminatorKey: "type" });

class Base {
    type = 1;
    whoAmI() { return "I am Base"; }
}

baseSchema.loadClass(Base);

class NumberTyped extends Base {
    type = 3;
    whoAmI() { return "I am NumberTyped"; }
}

class StringTyped extends Base {
    type = 4;
    whoAmI() { return "I am StringTyped"; }
}

const selfRefSchema = new Schema({
    self: { type: [baseSchema], required: true },
})

class SelfReferenceTyped extends Base {
    selfReferences = [];
    type = 5;
    whoAmI() { return "I am SelfReferenceTyped"; }
}

selfRefSchema.loadClass(SelfReferenceTyped);
baseSchema.discriminator(5, selfRefSchema);

const numberTypedSchema = new Schema({}).loadClass(NumberTyped);
const stringTypedSchema = new Schema({}).loadClass(StringTyped);
baseSchema.discriminator(1, numberTypedSchema);
baseSchema.discriminator(3, stringTypedSchema);

// Uncommenting these lines makes the script work as expected
//selfRefSchema.path("self").discriminator(1, numberTypedSchema.loadClass(NumberTyped));
//selfRefSchema.path("self").discriminator(3, stringTypedSchema.loadClass(StringTyped));

const containerSchema = new Schema({items: [baseSchema]});
const containerModel = mongoose.model('container', containerSchema);

void async function main() 
{
    await mongoose.connect("mongodb://127.0.0.1:27017/example");
    mongoose.connection.db.dropDatabase();

    let instance = await containerModel.create({ items: [{type: 1}] }); // "I am NumberTyped" - Works
    console.log(instance.items[0].whoAmI());

    instance = await containerModel.create({ items: [{type: 3}] }); // "I am StringTyped" Works
    console.log(instance.items[0].whoAmI());

    instance = await containerModel.create({ 
        items: [{type: 5, self: [{type: 1}, {type: 3}] }]
    });

    console.log(instance.items[0].whoAmI()); // "I am SelfReferenceTyped" - Works
    instance.items[0].self.forEach(item => console.log(item.whoAmI()));
    // Expected:
    // I am NumberTyped
    // I am StringTyped
    // Actual:
    // I am Base
    // I am Base

    await mongoose.disconnect();
}();

Expected Behavior

No response

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 11, 2024
@vkarpov15 vkarpov15 added this to the 7.6.9 milestone Jan 11, 2024
vkarpov15 added a commit that referenced this issue Jan 12, 2024
…h defined using Schema.prototype.discriminator

Fix #14245
vkarpov15 added a commit that referenced this issue Jan 13, 2024
fix(document): handle embedded recursive discriminators on nested path defined using Schema.prototype.discriminator
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

3 participants