Skip to content

Commit 1dc89bd

Browse files
[Typescript] Enum types (OpenAPITools#18531) (OpenAPITools#23921)
* [Typescript] Enum types (OpenAPITools#18531) * Add enum type generator argument * Generate tests * fix default sample test * Fix all typescript tests * Update docs * Added description for enum types * Set enum type as default * Set addition param for all tests except * test without option set --------- Co-authored-by: Kuzma <57258237+ksvirkou-hubspot@users.noreply.github.com>
1 parent 0c584f0 commit 1dc89bd

18 files changed

Lines changed: 55 additions & 41 deletions

File tree

bin/configs/typescript-consolidated-browser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ additionalProperties:
88
projectName: ts-petstore-client
99
moduleName: petstore
1010
supportsES6: true
11+
enumType: stringUnion

bin/configs/typescript-consolidated-deno.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ additionalProperties:
77
npmName: ts-petstore-client
88
projectName: ts-petstore-client
99
moduleName: petstore
10+
enumType: stringUnion

bin/configs/typescript-consolidated-jquery.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ additionalProperties:
77
npmName: ts-petstore-client
88
projectName: ts-petstore-client
99
moduleName: petstore
10+
#enumType: enum

bin/configs/typescript-consolidated-node-object-parameters.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ additionalProperties:
88
useObjectParameters: true
99
projectName: ts-petstore-client
1010
moduleName: petstore
11+
enumType: stringUnion

docs/generators/typescript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2424
|enumNameSuffix|Suffix that will be appended to all enum names.| |Enum|
2525
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'| |PascalCase|
2626
|enumPropertyNamingReplaceSpecialChar|Set to true to replace '-' and '+' symbols with 'minus_' and 'plus_' in enum of type string| |false|
27+
|enumType|Specify the enum type which should be used in the client code.|<dl><dt>**stringUnion**</dt><dd>Union of literal string types</dd><dt>**enum**</dt><dd>Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)</dd></dl>|enum|
2728
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response. With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enums are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
2829
|fileContentDataType|Specifies the type to use for the content of a file - i.e. Blob (Browser, Deno) / Buffer (node)| |Buffer|
2930
|framework|Specify the framework which should be used in the client code.|<dl><dt>**fetch-api**</dt><dd>fetch-api</dd><dt>**jquery**</dt><dd>jquery</dd></dl>|fetch-api|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
8080

8181
private static final String USE_OBJECT_PARAMS_SWITCH = "useObjectParameters";
8282
private static final String USE_OBJECT_PARAMS_DESC = "Use aggregate parameter objects as function arguments for api operations instead of passing each parameter as a separate function argument.";
83+
private static final String ENUM_TYPE_SWITCH = "enumType";
84+
private static final String ENUM_TYPE_SWITCH_DESC = "Specify the enum type which should be used in the client code.";
85+
private static final String[][] ENUM_TYPES = {{"stringUnion", "Union of literal string types"}, {"enum", "Typescript's [string enums](https://www.typescriptlang.org/docs/handbook/enums.html#string-enums)"}};
8386

8487
protected static final String TYPESCRIPT_MAJOR_VERSION_SWTICH = "typescriptMajorVersion";
8588
private static final String TYPESCRIPT_MAJOR_VERSION_DESC = "Specify the major version of TypeScript to use in the client code. Default is 5.";
@@ -159,6 +162,13 @@ public TypeScriptClientCodegen() {
159162

160163
cliOptions.add(platformOption);
161164

165+
CliOption enumTypeOption = new CliOption(TypeScriptClientCodegen.ENUM_TYPE_SWITCH, TypeScriptClientCodegen.ENUM_TYPE_SWITCH_DESC);
166+
for (String[] option : TypeScriptClientCodegen.ENUM_TYPES) {
167+
enumTypeOption.addEnum(option[0], option[1]);
168+
}
169+
enumTypeOption.defaultValue(ENUM_TYPES[1][0]);
170+
cliOptions.add(enumTypeOption);
171+
162172
// Set property naming to camelCase
163173
supportModelPropertyNaming(CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.camelCase);
164174

@@ -460,6 +470,15 @@ public void processOpts() {
460470
"http", httpLibName + ".ts"
461471
));
462472

473+
additionalProperties.putIfAbsent(ENUM_TYPE_SWITCH, ENUM_TYPES[1][0]);
474+
Object propEnumType = additionalProperties.get(ENUM_TYPE_SWITCH);
475+
476+
Map<String, Boolean> enumTypes = new HashMap<>();
477+
for (String[] option : ENUM_TYPES) {
478+
enumTypes.put(option[0], option[0].equals(propEnumType));
479+
}
480+
additionalProperties.put("enumTypes", enumTypes);
481+
463482
Object propPlatform = additionalProperties.get(PLATFORM_SWITCH);
464483
if (propPlatform == null) {
465484
propPlatform = "browser";

modules/openapi-generator/src/main/resources/typescript/model/model.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,41 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
8989

9090
{{#vars}}
9191
{{#isEnum}}
92+
{{#enumTypes}}
93+
{{#enum}}
9294
export enum {{classname}}{{enumName}} {
9395
{{#allowableValues}}
9496
{{#enumVars}}
9597
{{name}} = {{{value}}}{{^-last}},{{/-last}}
9698
{{/enumVars}}
9799
{{/allowableValues}}
98100
}
101+
{{/enum}}
102+
{{#stringUnion}}
103+
export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
104+
{{/stringUnion}}
105+
{{/enumTypes}}
99106
{{/isEnum}}
100107
{{/vars}}
101108

102109
{{/hasEnums}}
103110
{{/oneOf}}
104111
{{/isEnum}}
105112
{{#isEnum}}
113+
{{#enumTypes}}
114+
{{#enum}}
106115
export enum {{classname}} {
107116
{{#allowableValues}}
108117
{{#enumVars}}
109118
{{name}} = {{{value}}}{{^-last}},{{/-last}}
110119
{{/enumVars}}
111120
{{/allowableValues}}
112121
}
122+
{{/enum}}
123+
{{#stringUnion}}
124+
export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
125+
{{/stringUnion}}
126+
{{/enumTypes}}
113127
{{/isEnum}}
114128
{{/model}}
115129
{{/models}}

samples/openapi3/client/petstore/typescript/builds/browser/models/Order.ts

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/typescript/builds/browser/models/Pet.ts

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/typescript/builds/deno/models/Order.ts

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)