Skip to content

fix: Enum option cannot generate enums if values are not valid property names #1761

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

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,15 @@ export function tsEnumMember(value: string | number, metadata: { name?: string;
} else if (name[0] === "-") {
name = `ValueMinus${name.slice(1)}`;
}
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_");

const invalidCharMatch = name.match(JS_PROPERTY_INDEX_INVALID_CHARS_RE);
if (invalidCharMatch) {
if (invalidCharMatch[0] === name) {
name = `"${name}"`;
} else {
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_");
}
}
}

let member: ts.EnumMember;
Expand Down
14 changes: 14 additions & 0 deletions packages/openapi-typescript/test/node-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ export type operations = Record<string, never>;`,
"x-enumNames": ["Uno", "Dos", "Tres"],
"x-enumDescriptions": ["El número uno", "El número dos", "El número tres"],
},
InvalidPropertyNameChars: {
type: "string",
enum: ["=", "!=", ">", "~", "^", "TE=ST"],
},
},
},
},
Expand Down Expand Up @@ -660,6 +664,8 @@ export interface components {
XEnumVarnames: XEnumVarnames;
/** @enum {number} */
XEnumNames: XEnumNames;
/** @enum {string} */
InvalidPropertyNameChars: InvalidPropertyNameChars;
};
responses: never;
parameters: never;
Expand Down Expand Up @@ -700,6 +706,14 @@ export enum XEnumNames {
// El número tres
Tres = 3
}
export enum InvalidPropertyNameChars {
"=" = "=",
"!=" = "!=",
">" = ">",
"~" = "~",
"^" = "^",
TE_ST = "TE=ST"
}
export type operations = Record<string, never>;`,
options: { enum: true },
},
Expand Down