mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-24 10:19:06 +00:00
feat(typescript-angular): add flag for using string enums (#3464)
* feat(typescript): add flag for using string enums * fix: adjust templates * fix: repair logic * fix: remove sample * fix: add to v7 sample * fix: remove unneeded lines
This commit is contained in:
committed by
Esteban Gehring
parent
8b054e6f8e
commit
0e621dcc29
@@ -55,6 +55,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();
|
||||
|
||||
@@ -551,8 +554,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
|
||||
@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 {
|
||||
@@ -571,14 +573,14 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,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;
|
||||
@@ -57,6 +59,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
protected String modelSuffix = "";
|
||||
protected String modelFileSuffix = "";
|
||||
protected String fileNaming = "camelCase";
|
||||
protected Boolean stringEnums = false;
|
||||
|
||||
private boolean taggedUnions = false;
|
||||
|
||||
@@ -89,6 +92,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
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
|
||||
@@ -137,6 +141,15 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
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) {
|
||||
@@ -272,6 +285,14 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user