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

[Typescript] Top-level array don't get generated #2481

Open
SBrandeis opened this issue Jan 25, 2024 · 1 comment
Open

[Typescript] Top-level array don't get generated #2481

SBrandeis opened this issue Jan 25, 2024 · 1 comment

Comments

@SBrandeis
Copy link

SBrandeis commented Jan 25, 2024

Hello there

I'm trying to use quicktype-core to generate typescript interfaces from the following JSON schema.

The input JSON schema
{
	"$schema": "http://json-schema.org/draft-06/schema#",
	"title": "TextClassificationOutput",
	"type": "array",
	"items": {
		"type": "object",
		"title": "TextClassificationOutputElement",
		"properties": {
			"label": {
				"type": "string",
			},
			"score": {
				"type": "number",
			}
		},
		"required": ["label", "score"]
	}
}

However, the generated Typescript types are not what I am expecting:

  • The "top-level" array type is not generated
  • The name for the "inner" object definition does not match the title attribute
The generated Typescript code
export interface TextClassificationOutput {
    label: string;
    score: number;
    [property: string]: any;
}

I am expecting something like this instead:

export type TextClassificationOutput = TextClassificationOutputElement[];

export interface TextClassificationOutputElement {
    label: string;
    score: number;
    [property: string]: any;
}
The code generation script (typescript)
import type { SerializedRenderResult } from "quicktype-core";
import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";
import * as fs from "fs/promises";

async function generateTypescript(): Promise<SerializedRenderResult> {
        const schema = new JSONSchemaInput(new FetchingJSONSchemaStore());
	await schema.addSource({
		name: `text-generation-output`,
		schema: await fs.readFile(`./schema.json`, { encoding: "utf-8" }),
	});
	const inputData = new InputData();
	inputData.addInput(schema);

	return await quicktype({
		inputData,
		lang: "typescript",
		alphabetizeProperties: true,
		rendererOptions: {
			"just-types": true,
			"nice-property-names": true,
			"prefer-unions": true,
			"prefer-const-values": true,
			"explicit-unions": true,
			"runtime-typecheck": false,
		},
	});
}

I'm happy to contribute a fix to this, but I can't wrap my head around the naming attribution code, and I don't know where to start.
Some pointers would be greatly appreciated.

@SBrandeis SBrandeis changed the title Top-level array don't get generated [Typescript] Top-level array don't get generated Jan 25, 2024
@kmturley
Copy link

If you change:
name: `text-generation-output`,

to:
name: `#/definitions/`,

it should work!

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

No branches or pull requests

2 participants