Skip to content

Skip Schema prefix trimmin if --enum is used with --root-types-no-schema-prefix #2206

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

Open
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ export default function transformComponentsObject(componentsObject: ComponentsOb
conflictCounter++;
aliasName = `${componentKey}${changeCase.pascalCase(name)}_${conflictCounter}`;
}

const ref = ts.factory.createTypeReferenceNode(`components['${key}']['${name}']`);
if (ctx.rootTypesNoSchemaPrefix && key === "schemas") {
aliasName = aliasName.replace(componentKey, "");
// Skipping --root-types generation only for enums if --enum is set
// while still applying --root-types-no-schema-prefix to other types.
if (!(ctx.enum && item.enum !== undefined)) {
aliasName = aliasName.replace(componentKey, "");
}
Comment on lines +84 to +88
Copy link

@fluctus fluctus Mar 30, 2025

Choose a reason for hiding this comment

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

@sultaniman While this fixes the issue, it introduces an inconsistency in the generated types. Everything else will have the Schema prefix removed, but for enums, we’ll end up with both EnumName (the enum itself) and an unnecessary SchemaEnumName.

Since EnumName already serves as both a value and a TypeScript type due to the nature of enums, SchemaEnumName seems redundant. Wouldn't it make more sense to omit the root type for enums entirely when --root-types-no-schema-prefix is used? (That was my suggestion in the original PR.)

}
const typeAlias = ts.factory.createTypeAliasDeclaration(
/* modifiers */ tsModifiers({ export: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,74 @@ export type Error = components['schemas']['Error'];
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true },
},
],
[
"options > rootTypes: true and rootTypesNoSchemaPrefix: true and enum: true",
{
given: {
schemas: {
Item: {
type: "object",
required: ["name", "url"],
properties: {
name: { type: "string" },
url: { type: "string" },
},
},
Document: {
type: "object",
required: ["name", "size", "url"],
properties: {
name: { type: "string" },
size: { type: "number" },
url: { type: "string" },
},
},
Error: {
type: "object",
required: ["code", "message"],
properties: {
code: { type: "string" },
message: { type: "string" },
},
},
MyEnum: {
type: "string",
enum: ["first", "second", "last"],
},
},
},
want: `{
schemas: {
Item: {
name: string;
url: string;
};
Document: {
name: string;
size: number;
url: string;
};
Error: {
code: string;
message: string;
};
/** @enum {string} */
MyEnum: MyEnum;
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}
export type Item = components['schemas']['Item'];
export type Document = components['schemas']['Document'];
export type Error = components['schemas']['Error'];
export type SchemaMyEnum = components['schemas']['MyEnum'];
`,
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true, enum: true },
},
],
[
"transform > with transform object",
{
Expand Down