Skip to content

Commit

Permalink
fix(ts-input): Replace topLevelName for TypeScript input if first typ…
Browse files Browse the repository at this point in the history
…e is export default (#2550)
  • Loading branch information
inferrinizzard authored Apr 13, 2024
1 parent 8575cde commit a550841
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/quicktype-typescript-input/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,24 @@ export function schemaForTypeScriptSources(sourceFileNames: string[]): JSONSchem

const schema = generateSchema(program, "*", settings);
const uris: string[] = [];
let topLevelName: string | undefined = undefined;
let topLevelName: string = "";

// if there is a type that is `export default`, swap the corresponding ref
if (schema?.definitions?.default) {
const defaultDefinition = schema?.definitions?.default;
const matchingDefaultName = Object.entries(schema?.definitions ?? {}).find(
([_name, definition]) => (definition as Record<string, unknown>)["$ref"] === "#/definitions/default"
)?.[0];

if (matchingDefaultName) {
topLevelName = matchingDefaultName;
(defaultDefinition as Record<string, unknown>).title = topLevelName;

schema.definitions[matchingDefaultName] = defaultDefinition;
schema.definitions.default = { $ref: `#/definitions/${matchingDefaultName}` };
}
}

if (schema !== null && typeof schema === "object" && typeof schema.definitions === "object") {
for (const name of Object.getOwnPropertyNames(schema.definitions)) {
const definition = schema.definitions[name];
Expand All @@ -59,22 +76,18 @@ export function schemaForTypeScriptSources(sourceFileNames: string[]): JSONSchem

uris.push(`#/definitions/${name}`);

if (topLevelName === undefined) {
if (!topLevelName) {
if (typeof definition.title === "string") {
topLevelName = definition.title;
} else {
topLevelName = name;
}
} else {
topLevelName = "";
}
}
}
if (uris.length === 0) {
uris.push("#/definitions/");
}
if (topLevelName === undefined) {
topLevelName = "";
}

return { schema: JSON.stringify(schema), name: topLevelName, uris, isConverted: true };
}

0 comments on commit a550841

Please sign in to comment.