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 1 commit
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
dist
node_modules
coverage
mise.toml

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

# IntelliJ IDEA settings folder
/.idea
/.idea
3 changes: 3 additions & 0 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ if (args.includes("--redoc")) {
}
if (args.includes("--root-types-no-schema-prefix") && !args.includes("--root-types")) {
console.warn("--root-types-no-schema-prefix has no effect without --root-types flag");
if (args.includes("--enum")) {
console.warn("--root-types-no-schema-prefix has no effect when --enum used");
}
}

const flags = parser(args, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ 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") {
// We skip schema prefix replacement when --enum flag is present
if (ctx.rootTypesNoSchemaPrefix && key === "schemas" && !ctx.enum) {
aliasName = aliasName.replace(componentKey, "");
}
const typeAlias = ts.factory.createTypeAliasDeclaration(
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 SchemaItem = components['schemas']['Item'];
export type SchemaDocument = components['schemas']['Document'];
export type SchemaError = components['schemas']['Error'];
export type SchemaMyEnum = components['schemas']['MyEnum'];
`,
options: { ...DEFAULT_OPTIONS, rootTypes: true, rootTypesNoSchemaPrefix: true, enum: true },
},
],
[
"transform > with transform object",
{
Expand Down