Nested fields break the forestadmin UI using Mongoose

When using the agent and the Mongoose datasource with a collection with fields that have a nested object schema, the Forestadmin UI crashes when visiting that collection.

We are encountering this error on all Mongoose models defined with a schema like this one:

const cardSchema = new Schema(
	{
		game: {
			type: Schema.Types.ObjectId,
			ref: "Game",
			required: true,
			index: true,
		},
		gameSlug: { type: String, required: true, index: true },
		slug: { type: String, required: true, index: true },
		name: { type: String },
		test: {
			a: Number,
			b: Number,
		},
	},
	{ strict: false, strictQuery: false },
);

Here, it is the test field that is causing the error, making the entire UI unusable.
This part of the client-side code running in the browser throws an exception:

get filterActivated() {
    const e = this.collection?.rendering?.environment
      , t = !this.collection?.isSmart && this.isVirtual
      , i = !!e && this.lianaFeatures.isFeatureCompatible(f.FeaturesEnum.FilterSmartField, e);
    return this.isFilterAvailableForType && this.isFilterDisplayed && this.canFilter && (!this.capabilities || this.capabilities && this.capabilities.operators.length > 0) && (!t || i)
}

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘length’)

This happens because the value of this.capabilities.operators is undefined.

I am not sure if this issue is because of the schema introspection and what is returned to the forestadmin UI, or if it’s directly an issue on the UI side.

Using latest versions of the agent and mongoose datasource

Cross-reference, this issue here : Nested Mongoose fields make the Forestadmin UI crash? · Issue #1271 · ForestAdmin/agent-nodejs · GitHub

Hello @Tristan_Foureur,

Sadly I do not reproduce using the following versions in my package.json,

    "@forestadmin/agent": "1.60.0",
    "@forestadmin/datasource-mongoose": "1.11.1",
    "mongoose": "8.12.1"

Do you have any customizations on your Forest Admin collections ?

We’re using those same versions for the agent, and mongoose

	const agent = createAgent<Schema>({
		authSecret: process.env.FOREST_AUTH_SECRET,
		envSecret: process.env.FOREST_ENV_SECRET,
		isProduction: process.env.NODE_ENV === "production",
		typingsPath: `${__dirname}/typings.ts`,
	})

	const db = container.resolve(Mongo)
	const dataSource = createMongooseDataSource(db.mongoose, {
		flattenMode: "manual",
		flattenOptions: {
			List: {
				asModels: ["products"],
			},
		},
	})
	agent
		.addDataSource(dataSource)
	return agent

Any form of nested object that is not an array gives us this error

client-fb263c62c41cc5ff9d93df7a2b926ef1.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
    at get filterActivated (client-fb263c62c41cc5ff9d93df7a2b926ef1.js:1:230058)
    at vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:73645
    at e.untrack (vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:346771)
    at de.get (vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:73634)
    at nt.r [as filterActivated] (vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:71812)
    at client-fb263c62c41cc5ff9d93df7a2b926ef1.js:1:141276
    at Array.filter (<anonymous>)
    at get filterableFields (client-fb263c62c41cc5ff9d93df7a2b926ef1.js:1:141260)
    at vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:73645
    at e.untrack (vendor-407cd0e7b2f49e771e6364e9c681cb95.js:22:346771)

Do you experience the issue with the autoFlatten mode ? as it is the setup I tried to reproduce on:

agent.addDataSource(createMongooseDataSource(connection, { flattenMode: 'auto', }));