GithubHelp home page GithubHelp logo

blugavere / mongoose-to-swagger Goto Github PK

View Code? Open in Web Editor NEW

This project forked from giddyinc/mongoose-to-swagger

3.0 3.0 2.0 1.64 MB

Conversion library for transforming Mongoose schema objects into Swagger schema definitions.

JavaScript 3.46% TypeScript 96.54%

mongoose-to-swagger's People

Contributors

blugavere avatar dependabot[bot] avatar james1x0 avatar jatin-parate-mlveda avatar josh119891 avatar jylauril avatar ruffrey avatar x1frm avatar

Stargazers

 avatar  avatar  avatar

Forkers

for2425

mongoose-to-swagger's Issues

Maximum call stack size exceeded

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:

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

  2. 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:

Step 1: Add 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 ...
};

Step 2: Track recursive paths

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.

Cannot read properties of undefined (reading 'tree')

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 .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.