RangeError: Maximum call stack size exceeded
Closed this issue ยท 2 comments
๐ Bug Report
Infinite recursion leading to maximum call stack exceeded when a schema reference itself.
Reproduction
import fs from "node:fs";
import path from "node:path";
import { openapiToTsJsonSchema } from "openapi-ts-json-schema";
const schemaFile = path.join(__dirname, "./bug.OpenAPI.json");
if (!fs.existsSync(schemaFile)) { console.log("schemaFile does not exist", schemaFile); process.exit(1); }
async function main() {
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: schemaFile,
definitionPathsToGenerateFrom: ["components.schemas"],
});
console.log(outputPath);
}
main();
content of bug.OpenAPI.json
{
"openapi": "3.0.1",
"paths": {},
"components": {
"schemas": {
"DiagnosticInfo": {
"type": "object",
"properties": {
"InnerDiagnosticInfo": {
"$ref": "#/components/schemas/DiagnosticInfo"
}
}
}
}
}
}
Current Behavior
#/components/schemas/DiagnosticInfo
/home/etienne/projects/node-opcua-restapi/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:55
if (!ESCAPABLE.test(string)) {
^
RangeError: Maximum call stack size exceeded
at RegExp.test (<anonymous>)
at escape (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:55:18)
at quote (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:70:29)
at stringify (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:279:12)
at iteratee (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:208:16)
at Array.forEach (<anonymous>)
at object_stringify (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:241:8)
at stringify (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:298:9)
at iteratee (/project/node_modules/.pnpm/comment-json@4.2.3/node_modules/comment-json/src/stringify.js:208:16)
at Array.forEach (<anonymous>)
Expected behavior
- the converter should not crash and produce valid Typescript output.
Hi @erossignon,
thanks for opening the first on this project :)
So the issue seems definitely caused by the recursive JSON schema.
According to the tests, it should be possible handling recursive schemas with refHandling
option set to "import"
or "keep"
. Would they work for you?
The default inline refHandling
fails since it tries to build an infinitely nested structure.
Note that:
Circular $refs can be technically resolved with "import" refHandling option. But TS will stop the type recursion and type the schema as any (error ts(7022)).
Hi @erossignon! From v0.7.0 openapi-ts-json-schema
can inline-dereference recursive schemas interrupting the recursion on the second nesting level.