francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
103 stars 24 forks source link

Map of Child Schema throws "Maximum call stack size exceeded" #96

Closed www-chique closed 1 year ago

www-chique commented 2 years ago

Hey! Back again after a while with something. I'm using the latest version of the module (9.0.5).

The desired structure Before I show the Schema, I think it's easier to explain by taking a look at the Object structure itself:

{
    leaderboard: {
        // Contains different teams
        TeamA: {
            // Contains allTime and thisMonth
            allTime: [
                // Contains objects with names of some users
                {
                    name: "John",
                },
                {
                    name: "Alice",
                },
            ],
            thisMonth: [
                // Similar structure as allTime
            ],
        },
        TeamB: {
            // Similar structure as Team A
        },
    }
}

The schema

{
    ... [more stuff here]

    // Leaderboard
    leaderboard: {
        type: Map,
        of: new Schema(
            {
                allTime: [
                    new Schema(
                        {
                            name: {
                                type: String,
                                maxlength: 64,
                            },
                        },
                        {
                            timestamps: false,
                        }
                    ),
                ],
                thisMonth: [
                    new Schema(
                        {
                            name: {
                                type: String,
                                maxlength: 64,
                            },
                        },
                        {
                            timestamps: false,
                        }
                    ),
                ],
            },
            {
                _id: false,
                timestamps: false,
            }
        ),
    },
}

This throws "Maximum call stack size exceeded" error when you run "mtgen".

Any help would be appreciated, as life without mtgen is horrible.

francescov1 commented 2 years ago

Hey @www-chique! Thanks for submitting. I'll need a bit of time to dig into this since its a bit of an uncommon use case and the code has gotten pretty messy lately 😅 will update when I have more info.

www-chique commented 2 years ago

First, I want to acknowledge the fact that you've always been there whenever an issue appears. I very much appreciate you maintaining this project, and I can't imagine how hard it is to do it consistently over time.

Okay, back to the actual problem, it's not urgent at all. I found out a way to avoid this problem by structuring the schema in a different way.

I just thought I'll share the info, so it'll help the project in the future, if something similar happens in a different scenario. I don't have much knowledge, but I think this is similar to #28 and #63

francescov1 commented 2 years ago

@www-chique I appreciate the appraisal 🙏

Great to hear you found a work around, I took a crack at it last weekend but it seems that resolving this will need a bit of a refactor of the code. I have pretty limited time in the next month so I likely won't be able to get to it for a bit.

As a side note, the Maximum call stack size exceeded error occurs when the generator comes across a schema structure that it is not built to handle. I've been meaning to use a more clear error message, so will try to add this too as part of the refactor.

xDelph commented 1 year ago

Hi First of all, thanks for this package, really useful

Sadly i encounter the same kind bug here with something like UserSchema { right: RightSchema { feature: string access: AccessSchema { ... } }

Do you still work on this ?

francescov1 commented 1 year ago

Hi @xDelph, yes I still work on this. If you could provide a simple repro for your issue I would be happy to look into it. If your problem does not involve using a Map, can you open a new issue since its unrelated to this one.

xDelph commented 1 year ago

I will open a new issue, as it's not a map, but should be more a number of child dependance problem Thanks

Nik-Novak commented 1 year ago

This is still a problem for me

francescov1 commented 1 year ago

This should be resolved by https://github.com/francescov1/mongoose-tsgen/pull/115, but some of the more unusual use cases (multiple levels of schema & map nesting) should be validated. Release just went out (v9.1.1), so feel free to re-open this if there are still any associated issues!