Skip to content

Commit

Permalink
feat: Generate titles from definition names
Browse files Browse the repository at this point in the history
All root child definitions have a 'title' added to their definition if the opt-in definitionTitles option is set
  • Loading branch information
FoxxMD committed Dec 10, 2024
1 parent d500564 commit eaf3b87
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
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) {
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

0 comments on commit eaf3b87

Please sign in to comment.