mongoose-tsgen icon indicating copy to clipboard operation
mongoose-tsgen copied to clipboard

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

Open www-chique opened this issue 2 years ago • 3 comments

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.

www-chique avatar May 26 '22 11:05 www-chique

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.

francescov1 avatar May 28 '22 01:05 francescov1

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

www-chique avatar May 28 '22 22:05 www-chique

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

francescov1 avatar Jun 03 '22 00:06 francescov1

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 ?

xDelph avatar Jan 18 '23 15:01 xDelph

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.

francescov1 avatar Jan 18 '23 21:01 francescov1

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

xDelph avatar Jan 20 '23 08:01 xDelph

This is still a problem for me

Nik-Novak avatar May 27 '23 20:05 Nik-Novak

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!

francescov1 avatar May 31 '23 21:05 francescov1