Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximum call stack size exceeded #482

Open
mattem opened this issue Aug 11, 2022 · 3 comments
Open

Maximum call stack size exceeded #482

mattem opened this issue Aug 11, 2022 · 3 comments

Comments

@mattem
Copy link

mattem commented Aug 11, 2022

When running against the CircleCI configuration schema here,
https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/circleciconfig.json a max call stack size error is thrown.

This seems to be caused by the circular references on the logical definitions within the schema.

Using version 11.0.2 of json-schema-to-typescript

[
  RangeError: Maximum call stack size exceeded
      at memoized (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/lodash/lodash.js:10612:30)
      at /private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:260:81
      at Array.map (<anonymous>)
      at generateSetOperation (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:260:30)
      at generateRawType (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:249:20)
      at generateTypeUnmemoized (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:143:16)
      at memoized (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/lodash/lodash.js:10620:27)
      at /private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:271:72
      at Array.map (<anonymous>)
      at generateInterface (/private/var/tmp/_bazel_matt/1074e32c37c3b3e7bc10b426cb247bb7/sandbox/darwin-sandbox/129/execroot/__main__/bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/rosetta/src/types/circleci_dts__js_binary.sh.runfiles/__main__/node_modules/.aspect_rules_js/[email protected]/node_modules/json-schema-to-typescript/dist/src/generator.js:269:14)
]
@wawhal
Copy link

wawhal commented Sep 12, 2023

I'm facing the same. Any update on this?

EDIT: My issue was that it was getting into circular references because two types (one of which was included through a remote JSON schema) had the same definition name. I just added different titles to them and it worked fine for me.

@yorinasub17
Copy link

yorinasub17 commented Nov 20, 2023

I also ran into this and traced it down to the minimum reproducible example:

export const input = {
  definitions: {
    logical: {
      oneOf: [
        {
          type: 'string',
          description: 'oneOf string',
        },
        {
          type: 'object',
          description: 'oneOf object',
          properties: {
            not: {
              description: 'THIS IS PROBLEMATIC',
              $ref: '#/definitions/logical',
            },
          },
        },
      ],
    },
  },
  properties: {
    workflows: {
      type: 'object',
      description: 'workflows object',
      properties: {
        when: {
          description: 'workflows when',
          $ref: '#/definitions/logical',
        },
      },
    },
  },
  type: 'object',
}

After experimenting with the test suite, I identified that the issue is that when the recursive reference has a description, the code can not identify a standaloneName for the definition, and thus it ends up recursing in the generateType function.

However, if the description field is dropped from the recursive reference (the one set to THIS IS PROBLEMATIC), then it can generate without hitting the max callstack. I am not sure why that is the case, or how to fix it, but at least that appears to be what is causing the issue.

Note that it also works if the top level logical definition object has a title, which makes sense since that also sets a standaloneName on the object.


For anyone who wants to workaround this on the circleci config schema, you can do the following:

import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";

import { compile } from "json-schema-to-typescript";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const resp = await fetch("https://json.schemastore.org/circleciconfig.json");
const schema = await resp.json();

// Update the id, since it is used to generate the base type.
schema["$id"] = "CircleCIConfig";

// Assist json-schema-to-typescript by giving the problematic definition a title.
schema.definitions.logical.title = "logical";

const types = await compile(schema, "CircleCIConfig", {
  strictIndexSignatures: true,
});
fs.writeFileSync(path.join(__dirname, "circleci.d.ts"), types);

Note however that although this runs generation to completion, the resulting code is not compilable as it runs into #559 , so that patch is also required to get the circleci config to work.

@ndom91
Copy link

ndom91 commented Nov 23, 2024

@yorinasub17 just wanted to follow-up and say I ran into the same issue with the following openapi doc:

"Butler_API_Entities_Project": {
  "type": "object",
  "properties": {
    "slug": {
      "type": "string"
    },
    "owner": {
      "type": "string"
    },
    "parent_project": {
      "$ref": "#/components/schemas/Butler_API_Entities_Project"
    },
    "name": {
      "type": "string",
      "description": "Project Name"
    },
  },
}

As you can see the parent_project property doesn't have any other fields, but the cyclic reference still rbeaks the build with the "max call stack size exceeded" error 🤔

"fumadocs-core": "14.4.0",
"fumadocs-mdx": "11.1.1",
"fumadocs-openapi": "^5.5.10",
"fumadocs-twoslash": "^2.0.1",
"fumadocs-ui": "14.4.0",

EDIT: Ah just realized that fumadocs-openapi is using a fork of your library. I'll post it there 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants