blugavere / mongoose-to-swagger Goto Github PK
View Code? Open in Web Editor NEWThis project forked from giddyinc/mongoose-to-swagger
Conversion library for transforming Mongoose schema objects into Swagger schema definitions.
This project forked from giddyinc/mongoose-to-swagger
Conversion library for transforming Mongoose schema objects into Swagger schema definitions.
In reference to my old issue: giddyinc#64
The "Maximum call stack size exceeded" error usually indicates that there is a recursive loop running indefinitely. Given your Mongoose schema setup and the provided conversion code, it's highly likely that the infinite recursion is happening because of how the code handles nested schemas or references.
Let's try a step-by-step approach to debug and solve this:
Detect and Prevent Infinite Recursion: We can introduce a depth check or a recursive-path check to stop the recursion after a certain depth or if the function comes across the same path more than once.
Refactor the Recursive Calls: It's essential to handle the base cases and recursion properly to prevent such errors. If there is a cyclic reference in the Mongoose schema or any unintended recursive path, this will ensure it's caught.
Let's begin by adding a depth check:
To prevent infinite recursion, we'll add a depth
parameter to the recursive functions and increase it with each recursive call. If the depth surpasses a certain threshold, we'll stop the recursion.
const MAX_DEPTH = 10;
const mapSchemaTypeToFieldSchema = ({ key = null, value, props, omitFields, depth = 0 }) => {
if (depth > MAX_DEPTH) {
throw new Error(`Max recursion depth of ${MAX_DEPTH} exceeded`);
}
// ... rest of the function ...
if (swaggerType === 'array') {
// ... other parts of the code ...
const items = mapSchemaTypeToFieldSchema({ value: arraySchema || {}, props, omitFields, depth: depth + 1 });
meta.items = items;
}
// ... rest of the function ...
};
const getFieldsFromMongooseSchema = (schema, options, depth = 0) => {
if (depth > MAX_DEPTH) {
throw new Error(`Max recursion depth of ${MAX_DEPTH} exceeded`);
}
// ... rest of the function ...
};
Besides depth, another way to detect recursion is by tracking which paths you've visited. If a path is revisited, that means there's a cycle.
To achieve this, you can use an array or a set to track the paths that your function has visited:
const mapSchemaTypeToFieldSchema = ({ key = null, value, props, omitFields, depth = 0, visitedPaths = [] }) => {
if (visitedPaths.includes(key)) {
return { type: 'object' }; // or any default value to break the cycle
}
visitedPaths.push(key);
// ... rest of the function ...
};
const getFieldsFromMongooseSchema = (schema, options, depth = 0, visitedPaths = []) => {
// ... rest of the function ...
};
These changes should help detect and prevent infinite recursion. You may need to tweak the depth or adjust how you handle cyclic references based on your specific use case.
After implementing these changes, run your code. If it throws the "Max recursion depth exceeded" error, then you can be sure that the recursion depth is the issue. If it doesn't, but you get a different behavior, it might shed more light on the part of the schema causing the issue. Adjusting the MAX_DEPTH
value can also help in narrowing down the problem.
I am receiving the following error:
/home/xxxxxx/projects/xxxxxx/node_modules/mongoose-to-swagger/dist/index.js:139
const tree = schema.tree;
^
TypeError: Cannot read properties of undefined (reading 'tree')
at getFieldsFromMongooseSchema (/home/xxxxxx/projects/xxxxxx/node_modules/mongoose-to-swagger/dist/index.js:139:25)
at documentModel (/home/xxxxxx/projects/xxxxxx/node_modules/mongoose-to-swagger/dist/index.js:178:20)
at Object.<anonymous> (/home/xxxxxx/projects/xxxxxx/openapi/swagger.js:11:30)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
"mongoose-to-swagger": "^1.5.1",
"swagger-jsdoc": "^6.2.8",
I'm not sure if it matters, but the Mongooes schemas I am attempting to convert use discriminator
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.