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

feat: Generate titles from definition names #2127

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ By default, the command-line generator will use the `tsconfig.json` file in the
--markdown-description Generate `markdownDescription` in addition to `description`.
--functions <functions> How to handle functions. `fail` will throw an error. `comment` will add a comment. `hide` will treat the function like a NeverType or HiddenType.
(choices: "fail", "comment", "hide", default: "comment")
--definition-titles Generates titles from definition names
--minify Minify generated schema (default: false)
--unstable Do not sort properties
--strict-tuples Do not allow additional items on tuples
Expand Down
2 changes: 2 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Config {
additionalProperties?: boolean;
discriminatorType?: "json-schema" | "open-api";
functions?: FunctionOptions;
definitionTitles?: boolean;
}

export type CompletedConfig = Config & typeof DEFAULT_CONFIG;
Expand All @@ -36,4 +37,5 @@ export const DEFAULT_CONFIG: Omit<Required<Config>, "path" | "type" | "schemaId"
additionalProperties: false,
discriminatorType: "json-schema",
functions: "comment",
definitionTitles: false,
};
3 changes: 3 additions & 0 deletions src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ export class SchemaGenerator {
const name = child.getName();
if (!(name in definitions)) {
definitions[name] = this.typeFormatter.getDefinition(child.getType());
if (this.config?.definitionTitles) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the best place. Something inside getDefinition should be better. This doesn't work for deep objects/definitions, either.

definitions[name].title = name;
}
}
return definitions;
}, childDefinitions);
Expand Down
11 changes: 11 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ describe("config", () => {
}),
);

it(
"definition-titles",
assertSchema("definition-titles", {
type: "MyObject",
expose: "export",
topRef: false,
definitionTitles: true,
jsDoc: "none",
}),
);

it(
"jsdoc-complex-none",
assertSchema("jsdoc-complex-none", {
Expand Down
36 changes: 36 additions & 0 deletions test/config/definition-titles/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface ExportInterface {
exportValue: string;
}
export type ExportAlias = ExportInterface;

interface PrivateInterface {
privateValue: string;
}
type PrivateAlias = PrivateInterface;

interface MixedInterface {
mixedValue: ExportAlias;
}
export type MixedAlias = PrivateInterface;

export type PublicAnonymousTypeLiteral = {
publicValue: string;
};

type PrivateAnonymousTypeLiteral = {
privateValue: string;
};

export interface MyObject {
exportInterface: ExportInterface;
exportAlias: ExportAlias;

privateInterface: PrivateInterface;
privateAlias: PrivateAlias;

mixedInterface: MixedInterface;
mixedAlias: MixedAlias;

publicAnonymousTypeLiteral: PublicAnonymousTypeLiteral;
privateAnonymousTypeLiteral: PrivateAnonymousTypeLiteral;
}
122 changes: 122 additions & 0 deletions test/config/definition-titles/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"definitions": {
"ExportAlias": {
"$ref": "#/definitions/ExportInterface",
"title": "ExportAlias"
},
"ExportInterface": {
"additionalProperties": false,
"properties": {
"exportValue": {
"type": "string"
}
},
"required": [
"exportValue"
],
"title": "ExportInterface",
"type": "object"
},
"MixedAlias": {
"additionalProperties": false,
"properties": {
"privateValue": {
"type": "string"
}
},
"required": [
"privateValue"
],
"title": "MixedAlias",
"type": "object"
},
"PublicAnonymousTypeLiteral": {
"additionalProperties": false,
"properties": {
"publicValue": {
"type": "string"
}
},
"required": [
"publicValue"
],
"title": "PublicAnonymousTypeLiteral",
"type": "object"
}
},
"properties": {
"exportAlias": {
"$ref": "#/definitions/ExportAlias"
},
"exportInterface": {
"$ref": "#/definitions/ExportInterface"
},
"mixedAlias": {
"$ref": "#/definitions/MixedAlias"
},
"mixedInterface": {
"additionalProperties": false,
"properties": {
"mixedValue": {
"$ref": "#/definitions/ExportAlias"
}
},
"required": [
"mixedValue"
],
"type": "object"
},
"privateAlias": {
"additionalProperties": false,
"properties": {
"privateValue": {
"type": "string"
}
},
"required": [
"privateValue"
],
"type": "object"
},
"privateAnonymousTypeLiteral": {
"additionalProperties": false,
"properties": {
"privateValue": {
"type": "string"
}
},
"required": [
"privateValue"
],
"type": "object"
},
"privateInterface": {
"additionalProperties": false,
"properties": {
"privateValue": {
"type": "string"
}
},
"required": [
"privateValue"
],
"type": "object"
},
"publicAnonymousTypeLiteral": {
"$ref": "#/definitions/PublicAnonymousTypeLiteral"
}
},
"required": [
"exportInterface",
"exportAlias",
"privateInterface",
"privateAlias",
"mixedInterface",
"mixedAlias",
"publicAnonymousTypeLiteral",
"privateAnonymousTypeLiteral"
],
"type": "object"
}
2 changes: 2 additions & 0 deletions ts-json-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const args = new Command()
.choices(["fail", "comment", "hide"])
.default("comment"),
)
.option("--definition-titles", "Generate titles from defintion names", false)
.option("--minify", "Minify generated schema", false)
.option("--unstable", "Do not sort properties")
.option("--strict-tuples", "Do not allow additional items on tuples")
Expand All @@ -56,6 +57,7 @@ const args = new Command()

const config: Config = {
minify: args.minify,
definitionTitles: args.definitionTitles,
path: args.path,
tsconfig:
typeof args.tsconfig === "string" ? args.tsconfig : findConfigFile(process.cwd(), (f) => tsSys.fileExists(f)),
Expand Down
Loading