Open mattem opened 2 years ago
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.
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 https://github.com/bcherny/json-schema-to-typescript/issues/559 , so that patch is also required to get the circleci config to work.
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