Skip to content

Commit 3fa8dbe

Browse files
committed
Add a flag to skip Schema prefix when rendering root types
1 parent 1488304 commit 3fa8dbe

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

packages/openapi-typescript/bin/cli.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@ const HELP = `Usage
1111
$ openapi-typescript [input] [options]
1212
1313
Options
14-
--help Display this
15-
--version Display the version
16-
--redocly [path], -c Specify path to Redocly config (default: redocly.yaml)
17-
--output, -o Specify output file (if not specified in redocly.yaml)
18-
--enum Export true TS enums instead of unions
19-
--enum-values Export enum values as arrays
20-
--dedupe-enums Dedupe enum types when \`--enum=true\` is set
21-
--check Check that the generated types are up-to-date. (default: false)
22-
--export-type, -t Export top-level \`type\` instead of \`interface\`
23-
--immutable Generate readonly types
24-
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
25-
--empty-objects-unknown Generate \`unknown\` instead of \`Record<string, never>\` for empty objects
26-
--default-non-nullable Set to \`false\` to ignore default values when generating non-nullable types
14+
--help Display this
15+
--version Display the version
16+
--redocly [path], -c Specify path to Redocly config (default: redocly.yaml)
17+
--output, -o Specify output file (if not specified in redocly.yaml)
18+
--enum Export true TS enums instead of unions
19+
--enum-values Export enum values as arrays
20+
--dedupe-enums Dedupe enum types when \`--enum=true\` is set
21+
--check Check that the generated types are up-to-date. (default: false)
22+
--export-type, -t Export top-level \`type\` instead of \`interface\`
23+
--immutable Generate readonly types
24+
--additional-properties Treat schema objects as if \`additionalProperties: true\` is set
25+
--empty-objects-unknown Generate \`unknown\` instead of \`Record<string, never>\` for empty objects
26+
--default-non-nullable Set to \`false\` to ignore default values when generating non-nullable types
2727
--properties-required-by-default
28-
Treat schema objects as if \`required\` is set to all properties by default
29-
--array-length Generate tuples using array minItems / maxItems
30-
--path-params-as-types Convert paths to template literal types
31-
--alphabetize Sort object keys alphabetically
32-
--exclude-deprecated Exclude deprecated types
33-
--root-types (optional) Export schemas types at root level
28+
Treat schema objects as if \`required\` is set to all properties by default
29+
--array-length Generate tuples using array minItems / maxItems
30+
--path-params-as-types Convert paths to template literal types
31+
--alphabetize Sort object keys alphabetically
32+
--exclude-deprecated Exclude deprecated types
33+
--root-types (optional) Export schemas types at root level
34+
--root-types-no-schema-prefix (optional) Do not add "Schema" prefix to types at the root level (should be used with --root-types)
3435
`;
3536

3637
const OUTPUT_FILE = "FILE";
@@ -56,6 +57,9 @@ if (args.includes("-it")) {
5657
if (args.includes("--redoc")) {
5758
errorAndExit(`The --redoc config flag has been renamed to "--redocly" (or -c as shorthand).`);
5859
}
60+
if (args.includes("--root-types-no-schema-prefix") && !args.includes("--root-types")) {
61+
console.warn("--root-types-no-schema-prefix has no effect without --root-types flag");
62+
}
5963

6064
const flags = parser(args, {
6165
boolean: [
@@ -76,6 +80,7 @@ const flags = parser(args, {
7680
"immutable",
7781
"pathParamsAsTypes",
7882
"rootTypes",
83+
"rootTypesNoSchemaPrefix",
7984
],
8085
string: ["output", "redocly"],
8186
alias: {
@@ -136,6 +141,7 @@ async function generateSchema(schema, { redocly, silent = false }) {
136141
immutable: flags.immutable,
137142
pathParamsAsTypes: flags.pathParamsAsTypes,
138143
rootTypes: flags.rootTypes,
144+
rootTypesNoSchemaPrefix: flags.rootTypesNoSchemaPrefix,
139145
redocly,
140146
silent,
141147
}),

packages/openapi-typescript/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export default async function openapiTS(
8080
exportType: options.exportType ?? false,
8181
immutable: options.immutable ?? false,
8282
rootTypes: options.rootTypes ?? false,
83+
rootTypesNoSchemaPrefix: options.rootTypesNoSchemaPrefix ?? false,
8384
injectFooter: [],
8485
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
8586
postTransform: typeof options.postTransform === "function" ? options.postTransform : undefined,

packages/openapi-typescript/src/transform/components-object.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ export default function transformComponentsObject(componentsObject: ComponentsOb
7070
let aliasName = changeCase.pascalCase(singularizeComponentKey(key)) + changeCase.pascalCase(name);
7171
// Add counter suffix (e.g. "_2") if conflict in name
7272
let conflictCounter = 1;
73+
74+
let rootTypePrefix = changeCase.pascalCase(singularizeComponentKey(key));
75+
if (ctx.rootTypesNoSchemaPrefix && key.toLowerCase() == "schemas") {
76+
rootTypePrefix = "";
77+
}
78+
7379
while (rootTypeAliases[aliasName] !== undefined) {
7480
conflictCounter++;
75-
aliasName = `${changeCase.pascalCase(singularizeComponentKey(key))}${changeCase.pascalCase(name)}_${conflictCounter}`;
81+
aliasName = `${rootTypePrefix}${changeCase.pascalCase(name)}_${conflictCounter}`;
7682
}
7783
const ref = ts.factory.createTypeReferenceNode(`components['${key}']['${name}']`);
7884
const typeAlias = ts.factory.createTypeAliasDeclaration(

packages/openapi-typescript/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ export interface OpenAPITSOptions {
659659
propertiesRequiredByDefault?: boolean;
660660
/** (optional) Generate schema types at root level */
661661
rootTypes?: boolean;
662+
/** (optional) Do not add Schema prefix to types at root level */
663+
rootTypesNoSchemaPrefix?: boolean;
662664
/**
663665
* Configure Redocly for validation, schema fetching, and bundling
664666
* @see https://redocly.com/docs/cli/configuration/
@@ -691,6 +693,7 @@ export interface GlobalContext {
691693
postTransform: OpenAPITSOptions["postTransform"];
692694
propertiesRequiredByDefault: boolean;
693695
rootTypes: boolean;
696+
rootTypesNoSchemaPrefix: boolean;
694697
redoc: RedoclyConfig;
695698
silent: boolean;
696699
transform: OpenAPITSOptions["transform"];

0 commit comments

Comments
 (0)