Skip to content
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

[typescript-angular] add flag for using string enums #3464

Merged
merged 6 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"npmName": "@openapitools/typescript-angular-petstore",
"npmVersion": "1.0.0",
"npmRepository" : "https://skimdb.npmjs.com/registry",
"snapshot" : false
"stringEnums": true,
"npmRepository": "https://skimdb.npmjs.com/registry",
"snapshot": false
}
1 change: 1 addition & 0 deletions docs/generators/typescript-angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ sidebar_label: typescript-angular
|modelSuffix|The suffix of the generated model.| |null|
|modelFileSuffix|The suffix of the file of the generated model (model<suffix>.ts).| |null|
|fileNaming|Naming convention for the output files: 'camelCase', 'kebab-case'.| |camelCase|
|stringEnums|Generate string enums instead of objects for enum values.| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
protected String npmName = null;
protected String npmVersion = "1.0.0";

protected String enumSuffix = "Enum";
protected String classEnumSeparator = ".";

public AbstractTypeScriptClientCodegen() {
super();

Expand Down Expand Up @@ -549,8 +552,7 @@ public String toEnumVarName(String name, String datatype) {

@Override
public String toEnumName(CodegenProperty property) {
String enumName = toModelName(property.name) + "Enum";

String enumName = toModelName(property.name) + enumSuffix;
if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
Expand All @@ -569,14 +571,14 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// name enum with model name, e.g. StatusEnum => Pet.StatusEnum
for (CodegenProperty var : cm.vars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + "." + var.enumName);
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + classEnumSeparator + var.enumName);
}
}
if (cm.parent != null) {
for (CodegenProperty var : cm.allVars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum
.replace(var.enumName, cm.classname + "." + var.enumName);
.replace(var.enumName, cm.classname + classEnumSeparator + var.enumName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
public static final String MODEL_SUFFIX = "modelSuffix";
public static final String MODEL_FILE_SUFFIX = "modelFileSuffix";
public static final String FILE_NAMING = "fileNaming";
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 ngVersion = "7.0.0";
protected String npmRepository = null;
Expand All @@ -56,6 +58,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
protected String modelSuffix = "";
protected String modelFileSuffix = "";
protected String fileNaming = "camelCase";
protected Boolean stringEnums = false;

private boolean taggedUnions = false;

Expand Down Expand Up @@ -88,6 +91,7 @@ public TypeScriptAngularClientCodegen() {
this.cliOptions.add(new CliOption(MODEL_SUFFIX, "The suffix of the generated model."));
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts)."));
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums)));
}

@Override
Expand Down Expand Up @@ -136,6 +140,15 @@ public void processOpts() {
addNpmPackageGeneration(ngVersion);
}

if (additionalProperties.containsKey(STRING_ENUMS)) {
setStringEnums(Boolean.valueOf(additionalProperties.get(STRING_ENUMS).toString()));
additionalProperties.put("stringEnums", getStringEnums());
if (getStringEnums()) {
enumSuffix = "";
classEnumSeparator = "";
}
}

if (additionalProperties.containsKey(WITH_INTERFACES)) {
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
if (withInterfaces) {
Expand Down Expand Up @@ -271,6 +284,14 @@ private String getIndexDirectory() {
return indexPackage.replace('.', File.separatorChar);
}

public void setStringEnums(boolean value) {
stringEnums = value;
}

public Boolean getStringEnums() {
return stringEnums;
}

@Override
public boolean isDataTypeFile(final String dataType) {
return dataType != null && dataType.equals("Blob");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{{#stringEnums}}
export enum {{classname}} {
{{#allowableValues}}
{{#enumVars}}
{{name}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
};
{{/stringEnums}}
{{^stringEnums}}
export type {{classname}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};

export const {{classname}} = {
Expand All @@ -6,4 +16,5 @@ export const {{classname}} = {
{{name}}: {{{value}}} as {{classname}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
};
};
{{/stringEnums}}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{{#hasEnums}}

{{^stringEnums}}
export namespace {{classname}} {
{{/stringEnums}}
{{#vars}}
{{#isEnum}}
{{#stringEnums}}
export enum {{classname}}{{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{name}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
};
{{/stringEnums}}
{{^stringEnums}}
export type {{enumName}} = {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}} | {{/-last}}{{/enumVars}}{{/allowableValues}};
export const {{enumName}} = {
{{#allowableValues}}
Expand All @@ -11,6 +23,8 @@ export namespace {{classname}} {
{{/enumVars}}
{{/allowableValues}}
};
{{/stringEnums}}
{{/isEnum}}
{{/vars}}
}{{/hasEnums}}
{{^stringEnums}}}{{/stringEnums}}
{{/hasEnums}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

public class TypeScriptAngularClientOptionsProvider implements OptionsProvider {
public static final String SUPPORTS_ES6_VALUE = "false";
public static final String STRING_ENUMS_VALUE = "false";
public static final String SORT_PARAMS_VALUE = "false";
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
public static final String MODEL_PROPERTY_NAMING_VALUE = "camelCase";
Expand Down Expand Up @@ -52,6 +53,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(CodegenConstants.MODEL_PROPERTY_NAMING, MODEL_PROPERTY_NAMING_VALUE)
.put(CodegenConstants.SUPPORTS_ES6, SUPPORTS_ES6_VALUE)
.put(TypeScriptAngularClientCodegen.STRING_ENUMS, STRING_ENUMS_VALUE)
.put(TypeScriptAngularClientCodegen.NPM_NAME, NMP_NAME)
.put(TypeScriptAngularClientCodegen.NPM_VERSION, NMP_VERSION)
.put(TypeScriptAngularClientCodegen.SNAPSHOT, Boolean.FALSE.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setSupportsES6(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.SUPPORTS_ES6_VALUE));
times = 1;
clientCodegen.setStringEnums(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.STRING_ENUMS_VALUE));
times = 1;
clientCodegen.setPrependFormOrBodyParameters(Boolean.valueOf(TypeScriptAngularClientOptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
times = 1;
}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ export interface Order {
/**
* Order Status
*/
status?: Order.StatusEnum;
status?: OrderStatus;
complete?: boolean;
}
export namespace Order {
export type StatusEnum = 'placed' | 'approved' | 'delivered';
export const StatusEnum = {
Placed: 'placed' as StatusEnum,
Approved: 'approved' as StatusEnum,
Delivered: 'delivered' as StatusEnum
};
}
export enum OrderStatus {
Placed = 'placed',
Approved = 'approved',
Delivered = 'delivered'
};



Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ export interface Pet {
/**
* pet status in the store
*/
status?: Pet.StatusEnum;
}
export namespace Pet {
export type StatusEnum = 'available' | 'pending' | 'sold';
export const StatusEnum = {
Available: 'available' as StatusEnum,
Pending: 'pending' as StatusEnum,
Sold: 'sold' as StatusEnum
};
status?: PetStatus;
}
export enum PetStatus {
Available = 'available',
Pending = 'pending',
Sold = 'sold'
};



Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export namespace Order {
};
}


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export namespace Pet {
};
}