Skip to content

Commit 088c65c

Browse files
authored
[typescript-axios] Add stringEnums option (#11368)
* add stringEnums option * update templates * add export * update samples * update document * improve readability * remove unnecessary code * add config file for sample * add sample * update sample * remove enum variable form modelObjetEnum template because this variable is not used in modelStringEnum template. * change the indentation to be the same as modelGeneric template
1 parent b901f11 commit 088c65c

File tree

30 files changed

+2664
-426
lines changed

30 files changed

+2664
-426
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
generatorName: typescript-axios
2+
outputDir: samples/client/petstore/typescript-axios/builds/with-string-enums
3+
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-axios
5+
additionalProperties:
6+
stringEnums: true

docs/generators/typescript-axios.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3434
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
3535
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3636
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
37+
|stringEnums|Generate string enums instead of objects for enum values.| |false|
3738
|supportsES6|Generate code that conforms to ES6.| |false|
3839
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |false|
3940
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
3434
public static final String WITHOUT_PREFIX_ENUMS = "withoutPrefixEnums";
3535
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
3636
public static final String WITH_NODE_IMPORTS = "withNodeImports";
37+
public static final String STRING_ENUMS = "stringEnums";
38+
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";
3739

3840
protected String npmRepository = null;
41+
protected Boolean stringEnums = false;
3942

4043
private String tsModelPackage = "";
4144

@@ -57,6 +60,7 @@ public TypeScriptAxiosClientCodegen() {
5760
this.cliOptions.add(new CliOption(WITHOUT_PREFIX_ENUMS, "Don't prefix enum names with class names", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
5861
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
5962
this.cliOptions.add(new CliOption(WITH_NODE_IMPORTS, "Setting this property to true adds imports for NodeJS", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
63+
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
6064
// Templates have no mapping between formatted property names and original base names so use only "original" and remove this option
6165
removeOption(CodegenConstants.MODEL_PROPERTY_NAMING);
6266
}
@@ -127,6 +131,11 @@ public void processOpts() {
127131
}
128132
}
129133

134+
if (additionalProperties.containsKey(STRING_ENUMS)) {
135+
this.stringEnums = Boolean.parseBoolean(additionalProperties.get(STRING_ENUMS).toString());
136+
additionalProperties.put("stringEnums", this.stringEnums);
137+
}
138+
130139
if (additionalProperties.containsKey(NPM_NAME)) {
131140
addNpmPackageGeneration();
132141
}

modules/openapi-generator/src/main/resources/typescript-axios/modelEnum.mustache

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last
88
{{/isBoolean}}
99

1010
{{^isBoolean}}
11-
export enum {{classname}} {
12-
{{#allowableValues}}
13-
{{#enumVars}}
14-
{{#enumDescription}}
15-
/**
16-
* {{.}}
17-
*/
18-
{{/enumDescription}}
19-
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
20-
{{/enumVars}}
21-
{{/allowableValues}}
22-
}
11+
{{^stringEnums}}
12+
{{>modelObjectEnum}}
13+
{{/stringEnums}}
14+
{{#stringEnums}}
15+
{{>modelStringEnum}}
16+
{{/stringEnums}}
2317
{{/isBoolean}}

modules/openapi-generator/src/main/resources/typescript-axios/modelGeneric.mustache

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
2323

2424
{{#vars}}
2525
{{#isEnum}}
26+
{{#stringEnums}}
2627
/**
2728
* @export
2829
* @enum {string}
@@ -39,6 +40,23 @@ export enum {{enumName}} {
3940
{{/enumVars}}
4041
{{/allowableValues}}
4142
}
43+
{{/stringEnums}}
44+
{{^stringEnums}}
45+
export const {{enumName}} = {
46+
{{#allowableValues}}
47+
{{#enumVars}}
48+
{{#enumDescription}}
49+
/**
50+
* {{.}}
51+
*/
52+
{{/enumDescription}}
53+
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
54+
{{/enumVars}}
55+
{{/allowableValues}}
56+
} as const;
57+
58+
export type {{enumName}} = typeof {{enumName}}[keyof typeof {{enumName}}];
59+
{{/stringEnums}}
4260
{{/isEnum}}
4361
{{/vars}}
4462
{{/hasEnums}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const {{classname}} = {
2+
{{#allowableValues}}
3+
{{#enumVars}}
4+
{{#enumDescription}}
5+
/**
6+
* {{.}}
7+
*/
8+
{{/enumDescription}}
9+
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
10+
{{/enumVars}}
11+
{{/allowableValues}}
12+
} as const;
13+
14+
export type {{classname}} = typeof {{classname}}[keyof typeof {{classname}}];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export enum {{classname}} {
2+
{{#allowableValues}}
3+
{{#enumVars}}
4+
{{#enumDescription}}
5+
/**
6+
* {{.}}
7+
*/
8+
{{/enumDescription}}
9+
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
10+
{{/enumVars}}
11+
{{/allowableValues}}
12+
}

samples/client/petstore/typescript-axios/builds/composed-schemas/api.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,14 @@ export interface Dog {
7979
'breed'?: DogBreedEnum;
8080
}
8181

82-
/**
83-
* @export
84-
* @enum {string}
85-
*/
86-
export enum DogBreedEnum {
87-
Dingo = 'Dingo',
88-
Husky = 'Husky',
89-
Retriever = 'Retriever',
90-
Shepherd = 'Shepherd'
91-
}
82+
export const DogBreedEnum = {
83+
Dingo: 'Dingo',
84+
Husky: 'Husky',
85+
Retriever: 'Retriever',
86+
Shepherd: 'Shepherd'
87+
} as const;
88+
89+
export type DogBreedEnum = typeof DogBreedEnum[keyof typeof DogBreedEnum];
9290

9391
/**
9492
*
@@ -110,16 +108,14 @@ export interface DogAllOf {
110108
'breed'?: DogAllOfBreedEnum;
111109
}
112110

113-
/**
114-
* @export
115-
* @enum {string}
116-
*/
117-
export enum DogAllOfBreedEnum {
118-
Dingo = 'Dingo',
119-
Husky = 'Husky',
120-
Retriever = 'Retriever',
121-
Shepherd = 'Shepherd'
122-
}
111+
export const DogAllOfBreedEnum = {
112+
Dingo: 'Dingo',
113+
Husky: 'Husky',
114+
Retriever: 'Retriever',
115+
Shepherd: 'Shepherd'
116+
} as const;
117+
118+
export type DogAllOfBreedEnum = typeof DogAllOfBreedEnum[keyof typeof DogAllOfBreedEnum];
123119

124120
/**
125121
*
@@ -173,14 +169,12 @@ export interface PetByType {
173169
'hunts'?: boolean;
174170
}
175171

176-
/**
177-
* @export
178-
* @enum {string}
179-
*/
180-
export enum PetByTypePetTypeEnum {
181-
Cat = 'Cat',
182-
Dog = 'Dog'
183-
}
172+
export const PetByTypePetTypeEnum = {
173+
Cat: 'Cat',
174+
Dog: 'Dog'
175+
} as const;
176+
177+
export type PetByTypePetTypeEnum = typeof PetByTypePetTypeEnum[keyof typeof PetByTypePetTypeEnum];
184178

185179

186180
/**

samples/client/petstore/typescript-axios/builds/default/api.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ export interface Order {
109109
'complete'?: boolean;
110110
}
111111

112-
/**
113-
* @export
114-
* @enum {string}
115-
*/
116-
export enum OrderStatusEnum {
117-
Placed = 'placed',
118-
Approved = 'approved',
119-
Delivered = 'delivered'
120-
}
112+
export const OrderStatusEnum = {
113+
Placed: 'placed',
114+
Approved: 'approved',
115+
Delivered: 'delivered'
116+
} as const;
117+
118+
export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum];
121119

122120
/**
123121
* A pet for sale in the pet store
@@ -163,15 +161,13 @@ export interface Pet {
163161
'status'?: PetStatusEnum;
164162
}
165163

166-
/**
167-
* @export
168-
* @enum {string}
169-
*/
170-
export enum PetStatusEnum {
171-
Available = 'available',
172-
Pending = 'pending',
173-
Sold = 'sold'
174-
}
164+
export const PetStatusEnum = {
165+
Available: 'available',
166+
Pending: 'pending',
167+
Sold: 'sold'
168+
} as const;
169+
170+
export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum];
175171

176172
/**
177173
* A tag for a pet

samples/client/petstore/typescript-axios/builds/es6-target/api.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ export interface Order {
109109
'complete'?: boolean;
110110
}
111111

112-
/**
113-
* @export
114-
* @enum {string}
115-
*/
116-
export enum OrderStatusEnum {
117-
Placed = 'placed',
118-
Approved = 'approved',
119-
Delivered = 'delivered'
120-
}
112+
export const OrderStatusEnum = {
113+
Placed: 'placed',
114+
Approved: 'approved',
115+
Delivered: 'delivered'
116+
} as const;
117+
118+
export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum];
121119

122120
/**
123121
* A pet for sale in the pet store
@@ -163,15 +161,13 @@ export interface Pet {
163161
'status'?: PetStatusEnum;
164162
}
165163

166-
/**
167-
* @export
168-
* @enum {string}
169-
*/
170-
export enum PetStatusEnum {
171-
Available = 'available',
172-
Pending = 'pending',
173-
Sold = 'sold'
174-
}
164+
export const PetStatusEnum = {
165+
Available: 'available',
166+
Pending: 'pending',
167+
Sold: 'sold'
168+
} as const;
169+
170+
export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum];
175171

176172
/**
177173
* A tag for a pet

0 commit comments

Comments
 (0)