Skip to content

Commit bfc784e

Browse files
[Typescript] Enum types (#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
1 parent c289982 commit bfc784e

20 files changed

Lines changed: 57 additions & 51 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: stringUnion

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 enum's 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
@@ -74,6 +74,9 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
7474

7575
private static final String USE_OBJECT_PARAMS_SWITCH = "useObjectParameters";
7676
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.";
77+
private static final String ENUM_TYPE_SWITCH = "enumType";
78+
private static final String ENUM_TYPE_SWITCH_DESC = "Specify the enum type which should be used in the client code.";
79+
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)"}};
7780

7881
private final Map<String, String> frameworkToHttpLibMap;
7982

@@ -135,6 +138,13 @@ public TypeScriptClientCodegen() {
135138

136139
cliOptions.add(platformOption);
137140

141+
CliOption enumTypeOption = new CliOption(TypeScriptClientCodegen.ENUM_TYPE_SWITCH, TypeScriptClientCodegen.ENUM_TYPE_SWITCH_DESC);
142+
for (String[] option : TypeScriptClientCodegen.ENUM_TYPES) {
143+
enumTypeOption.addEnum(option[0], option[1]);
144+
}
145+
enumTypeOption.defaultValue(ENUM_TYPES[1][0]);
146+
cliOptions.add(enumTypeOption);
147+
138148
// Set property naming to camelCase
139149
supportModelPropertyNaming(CodegenConstants.MODEL_PROPERTY_NAMING_TYPE.camelCase);
140150

@@ -426,6 +436,15 @@ public void processOpts() {
426436
"http", httpLibName + ".ts"
427437
));
428438

439+
additionalProperties.putIfAbsent(ENUM_TYPE_SWITCH, ENUM_TYPES[1][0]);
440+
Object propEnumType = additionalProperties.get(ENUM_TYPE_SWITCH);
441+
442+
Map<String, Boolean> enumTypes = new HashMap<>();
443+
for (String[] option : ENUM_TYPES) {
444+
enumTypes.put(option[0], option[0].equals(propEnumType));
445+
}
446+
additionalProperties.put("enumTypes", enumTypes);
447+
429448
Object propPlatform = additionalProperties.get(PLATFORM_SWITCH);
430449
if (propPlatform == null) {
431450
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
@@ -71,26 +71,40 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
7171

7272
{{#vars}}
7373
{{#isEnum}}
74+
{{#enumTypes}}
75+
{{#enum}}
7476
export enum {{classname}}{{enumName}} {
7577
{{#allowableValues}}
7678
{{#enumVars}}
7779
{{name}} = {{{value}}}{{^-last}},{{/-last}}
7880
{{/enumVars}}
7981
{{/allowableValues}}
8082
}
83+
{{/enum}}
84+
{{#stringUnion}}
85+
export type {{classname}}{{enumName}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
86+
{{/stringUnion}}
87+
{{/enumTypes}}
8188
{{/isEnum}}
8289
{{/vars}}
8390

8491
{{/hasEnums}}
8592
{{/isEnum}}
8693
{{#isEnum}}
94+
{{#enumTypes}}
95+
{{#enum}}
8796
export enum {{classname}} {
8897
{{#allowableValues}}
8998
{{#enumVars}}
9099
{{name}} = {{{value}}}{{^-last}},{{/-last}}
91100
{{/enumVars}}
92101
{{/allowableValues}}
93102
}
103+
{{/enum}}
104+
{{#stringUnion}}
105+
export type {{classname}} ={{#allowableValues}}{{#values}} "{{.}}" {{^-last}}|{{/-last}}{{/values}}{{/allowableValues}};
106+
{{/stringUnion}}
107+
{{/enumTypes}}
94108
{{/isEnum}}
95109
{{/model}}
96110
{{/models}}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,5 @@ export class Order {
7575
}
7676

7777

78-
export enum OrderStatusEnum {
79-
Placed = 'placed',
80-
Approved = 'approved',
81-
Delivered = 'delivered'
82-
}
78+
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;
8379

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,5 @@ export class Pet {
7777
}
7878

7979

80-
export enum PetStatusEnum {
81-
Available = 'available',
82-
Pending = 'pending',
83-
Sold = 'sold'
84-
}
80+
export type PetStatusEnum = "available" | "pending" | "sold" ;
8581

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,5 @@ export class Order {
7575
}
7676

7777

78-
export enum OrderStatusEnum {
79-
Placed = 'placed',
80-
Approved = 'approved',
81-
Delivered = 'delivered'
82-
}
78+
export type OrderStatusEnum = "placed" | "approved" | "delivered" ;
8379

0 commit comments

Comments
 (0)