Skip to content

Commit 3392361

Browse files
committed
Skip root types generation for enum and still trim Schema prefix if needed
1 parent 31054dc commit 3392361

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

Diff for: .gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
dist
44
node_modules
55
coverage
6-
mise.toml
76

87
packages/openapi-typescript/test/fixtures/cli-outputs/out
98

109
# IntelliJ IDEA settings folder
11-
/.idea
10+
/.idea

Diff for: packages/openapi-typescript/bin/cli.js

-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ if (args.includes("--redoc")) {
6161
}
6262
if (args.includes("--root-types-no-schema-prefix") && !args.includes("--root-types")) {
6363
console.warn("--root-types-no-schema-prefix has no effect without --root-types flag");
64-
if (args.includes("--enum")) {
65-
console.warn("--root-types-no-schema-prefix has no effect when --enum used");
66-
}
6764
}
6865

6966
const flags = parser(args, {

Diff for: packages/openapi-typescript/src/transform/components-object.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ export default function transformComponentsObject(componentsObject: ComponentsOb
8080
}
8181

8282
const ref = ts.factory.createTypeReferenceNode(`components['${key}']['${name}']`);
83-
// We skip schema prefix replacement when --enum flag is present
84-
if (ctx.rootTypesNoSchemaPrefix && key === "schemas" && !ctx.enum) {
85-
aliasName = aliasName.replace(componentKey, "");
83+
if (ctx.rootTypesNoSchemaPrefix && key === "schemas") {
84+
// Skipping --root-types generation only for enums if --enum is set
85+
// while still applying --root-types-no-schema-prefix to other types.
86+
if (!(ctx.enum && item.enum !== undefined)) {
87+
aliasName = aliasName.replace(componentKey, "");
88+
}
8689
}
8790
const typeAlias = ts.factory.createTypeAliasDeclaration(
8891
/* modifiers */ tsModifiers({ export: true }),

Diff for: packages/openapi-typescript/test/transform/components-object.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,9 @@ export type Error = components['schemas']['Error'];
820820
headers: never;
821821
pathItems: never;
822822
}
823-
export type SchemaItem = components['schemas']['Item'];
824-
export type SchemaDocument = components['schemas']['Document'];
825-
export type SchemaError = components['schemas']['Error'];
823+
export type Item = components['schemas']['Item'];
824+
export type Document = components['schemas']['Document'];
825+
export type Error = components['schemas']['Error'];
826826
export type SchemaMyEnum = components['schemas']['MyEnum'];
827827
`,
828828
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true, enum: true },

0 commit comments

Comments
 (0)