Skip to content

Commit 31054dc

Browse files
committed
Do not trim Schema prefix when --enum is used with --root-types-no-schema-prefix
1 parent 055f8f1 commit 31054dc

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

.gitignore

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

78
packages/openapi-typescript/test/fixtures/cli-outputs/out
89

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

packages/openapi-typescript/bin/cli.js

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ 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+
}
6467
}
6568

6669
const flags = parser(args, {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ export default function transformComponentsObject(componentsObject: ComponentsOb
7878
conflictCounter++;
7979
aliasName = `${componentKey}${changeCase.pascalCase(name)}_${conflictCounter}`;
8080
}
81+
8182
const ref = ts.factory.createTypeReferenceNode(`components['${key}']['${name}']`);
82-
if (ctx.rootTypesNoSchemaPrefix && key === "schemas") {
83+
// We skip schema prefix replacement when --enum flag is present
84+
if (ctx.rootTypesNoSchemaPrefix && key === "schemas" && !ctx.enum) {
8385
aliasName = aliasName.replace(componentKey, "");
8486
}
8587
const typeAlias = ts.factory.createTypeAliasDeclaration(

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

+68
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,74 @@ export type Error = components['schemas']['Error'];
760760
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true },
761761
},
762762
],
763+
[
764+
"options > rootTypes: true and rootTypesNoSchemaPrefix: true and enum: true",
765+
{
766+
given: {
767+
schemas: {
768+
Item: {
769+
type: "object",
770+
required: ["name", "url"],
771+
properties: {
772+
name: { type: "string" },
773+
url: { type: "string" },
774+
},
775+
},
776+
Document: {
777+
type: "object",
778+
required: ["name", "size", "url"],
779+
properties: {
780+
name: { type: "string" },
781+
size: { type: "number" },
782+
url: { type: "string" },
783+
},
784+
},
785+
Error: {
786+
type: "object",
787+
required: ["code", "message"],
788+
properties: {
789+
code: { type: "string" },
790+
message: { type: "string" },
791+
},
792+
},
793+
MyEnum: {
794+
type: "string",
795+
enum: ["first", "second", "last"],
796+
},
797+
},
798+
},
799+
want: `{
800+
schemas: {
801+
Item: {
802+
name: string;
803+
url: string;
804+
};
805+
Document: {
806+
name: string;
807+
size: number;
808+
url: string;
809+
};
810+
Error: {
811+
code: string;
812+
message: string;
813+
};
814+
/** @enum {string} */
815+
MyEnum: MyEnum;
816+
};
817+
responses: never;
818+
parameters: never;
819+
requestBodies: never;
820+
headers: never;
821+
pathItems: never;
822+
}
823+
export type SchemaItem = components['schemas']['Item'];
824+
export type SchemaDocument = components['schemas']['Document'];
825+
export type SchemaError = components['schemas']['Error'];
826+
export type SchemaMyEnum = components['schemas']['MyEnum'];
827+
`,
828+
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true, enum: true },
829+
},
830+
],
763831
[
764832
"transform > with transform object",
765833
{

0 commit comments

Comments
 (0)