Skip to content

Commit

Permalink
[typescript-axios] Add stringEnums option (#11368)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ty-v1 authored Jan 29, 2022
1 parent b901f11 commit 088c65c
Show file tree
Hide file tree
Showing 30 changed files with 2,664 additions and 426 deletions.
6 changes: 6 additions & 0 deletions bin/configs/typescript-axios-with-string-enums.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: typescript-axios
outputDir: samples/client/petstore/typescript-axios/builds/with-string-enums
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/typescript-axios
additionalProperties:
stringEnums: true
1 change: 1 addition & 0 deletions docs/generators/typescript-axios.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|snapshot|When setting this property to true, the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm| |false|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|stringEnums|Generate string enums instead of objects for enum values.| |false|
|supportsES6|Generate code that conforms to ES6.| |false|
|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|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodege
public static final String WITHOUT_PREFIX_ENUMS = "withoutPrefixEnums";
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
public static final String WITH_NODE_IMPORTS = "withNodeImports";
public static final String STRING_ENUMS = "stringEnums";
public static final String STRING_ENUMS_DESC = "Generate string enums instead of objects for enum values.";

protected String npmRepository = null;
protected Boolean stringEnums = false;

private String tsModelPackage = "";

Expand All @@ -57,6 +60,7 @@ public TypeScriptAxiosClientCodegen() {
this.cliOptions.add(new CliOption(WITHOUT_PREFIX_ENUMS, "Don't prefix enum names with class names", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
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()));
this.cliOptions.add(new CliOption(WITH_NODE_IMPORTS, "Setting this property to true adds imports for NodeJS", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
// Templates have no mapping between formatted property names and original base names so use only "original" and remove this option
removeOption(CodegenConstants.MODEL_PROPERTY_NAMING);
}
Expand Down Expand Up @@ -127,6 +131,11 @@ public void processOpts() {
}
}

if (additionalProperties.containsKey(STRING_ENUMS)) {
this.stringEnums = Boolean.parseBoolean(additionalProperties.get(STRING_ENUMS).toString());
additionalProperties.put("stringEnums", this.stringEnums);
}

if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last
{{/isBoolean}}

{{^isBoolean}}
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{^stringEnums}}
{{>modelObjectEnum}}
{{/stringEnums}}
{{#stringEnums}}
{{>modelStringEnum}}
{{/stringEnums}}
{{/isBoolean}}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{

{{#vars}}
{{#isEnum}}
{{#stringEnums}}
/**
* @export
* @enum {string}
Expand All @@ -39,6 +40,23 @@ export enum {{enumName}} {
{{/enumVars}}
{{/allowableValues}}
}
{{/stringEnums}}
{{^stringEnums}}
export const {{enumName}} = {
{{#allowableValues}}
{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
} as const;

export type {{enumName}} = typeof {{enumName}}[keyof typeof {{enumName}}];
{{/stringEnums}}
{{/isEnum}}
{{/vars}}
{{/hasEnums}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const {{classname}} = {
{{#allowableValues}}
{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
} as const;

export type {{classname}} = typeof {{classname}}[keyof typeof {{classname}}];
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{#enumDescription}}
/**
* {{.}}
*/
{{/enumDescription}}
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,14 @@ export interface Dog {
'breed'?: DogBreedEnum;
}

/**
* @export
* @enum {string}
*/
export enum DogBreedEnum {
Dingo = 'Dingo',
Husky = 'Husky',
Retriever = 'Retriever',
Shepherd = 'Shepherd'
}
export const DogBreedEnum = {
Dingo: 'Dingo',
Husky: 'Husky',
Retriever: 'Retriever',
Shepherd: 'Shepherd'
} as const;

export type DogBreedEnum = typeof DogBreedEnum[keyof typeof DogBreedEnum];

/**
*
Expand All @@ -110,16 +108,14 @@ export interface DogAllOf {
'breed'?: DogAllOfBreedEnum;
}

/**
* @export
* @enum {string}
*/
export enum DogAllOfBreedEnum {
Dingo = 'Dingo',
Husky = 'Husky',
Retriever = 'Retriever',
Shepherd = 'Shepherd'
}
export const DogAllOfBreedEnum = {
Dingo: 'Dingo',
Husky: 'Husky',
Retriever: 'Retriever',
Shepherd: 'Shepherd'
} as const;

export type DogAllOfBreedEnum = typeof DogAllOfBreedEnum[keyof typeof DogAllOfBreedEnum];

/**
*
Expand Down Expand Up @@ -173,14 +169,12 @@ export interface PetByType {
'hunts'?: boolean;
}

/**
* @export
* @enum {string}
*/
export enum PetByTypePetTypeEnum {
Cat = 'Cat',
Dog = 'Dog'
}
export const PetByTypePetTypeEnum = {
Cat: 'Cat',
Dog: 'Dog'
} as const;

export type PetByTypePetTypeEnum = typeof PetByTypePetTypeEnum[keyof typeof PetByTypePetTypeEnum];


/**
Expand Down
32 changes: 14 additions & 18 deletions samples/client/petstore/typescript-axios/builds/default/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ export interface Order {
'complete'?: boolean;
}

/**
* @export
* @enum {string}
*/
export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export const OrderStatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
} as const;

export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum];

/**
* A pet for sale in the pet store
Expand Down Expand Up @@ -163,15 +161,13 @@ export interface Pet {
'status'?: PetStatusEnum;
}

/**
* @export
* @enum {string}
*/
export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export const PetStatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
} as const;

export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum];

/**
* A tag for a pet
Expand Down
32 changes: 14 additions & 18 deletions samples/client/petstore/typescript-axios/builds/es6-target/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ export interface Order {
'complete'?: boolean;
}

/**
* @export
* @enum {string}
*/
export enum OrderStatusEnum {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
}
export const OrderStatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
} as const;

export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum];

/**
* A pet for sale in the pet store
Expand Down Expand Up @@ -163,15 +161,13 @@ export interface Pet {
'status'?: PetStatusEnum;
}

/**
* @export
* @enum {string}
*/
export enum PetStatusEnum {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
}
export const PetStatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
} as const;

export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum];

/**
* A tag for a pet
Expand Down
Loading

0 comments on commit 088c65c

Please sign in to comment.