Skip to content

Commit

Permalink
[dart] Fix and improve enumVar naming/generation
Browse files Browse the repository at this point in the history
* use same handling for all 3 generators
* allow `updateEnumVarsWithExtensions` to have access to the data type
* improve `x-enum-values` handling and add supprt for other enum vendor extensions
* remove duplicate and outdated tests from `DartDioModelTest`
* add more tests to `DartModelTest`
* no longer force lowercase enums for plain dart generator (breaking)
* this change also removes the trailing underscore from plain dart generator (breaking)
  • Loading branch information
kuhnroyal committed Dec 9, 2020
1 parent 251fa34 commit 0e4f449
Show file tree
Hide file tree
Showing 34 changed files with 411 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
List<Object> values = (List<Object>) allowableValues.get("values");
List<Map<String, Object>> enumVars = buildEnumVars(values, cm.dataType);
// if "x-enum-varnames" or "x-enum-descriptions" defined, update varnames
updateEnumVarsWithExtensions(enumVars, cm.getVendorExtensions());
updateEnumVarsWithExtensions(enumVars, cm.getVendorExtensions(), cm.dataType);
cm.allowableValues.put("enumVars", enumVars);
}

Expand Down Expand Up @@ -5441,7 +5441,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
if (referencedSchema.isPresent()) {
extensions = referencedSchema.get().getExtensions();
}
updateEnumVarsWithExtensions(enumVars, extensions);
updateEnumVarsWithExtensions(enumVars, extensions, dataType);
allowableValues.put("enumVars", enumVars);

// handle default value for enum, e.g. available => StatusEnum.AVAILABLE
Expand Down Expand Up @@ -5494,7 +5494,7 @@ protected List<Map<String, Object>> buildEnumVars(List<Object> values, String da
return enumVars;
}

protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions, String dataType) {
if (vendorExtensions != null) {
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-varnames", "name");
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-descriptions", "enumDescription");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.SupportingFile;
Expand Down Expand Up @@ -354,6 +352,10 @@ public String modelDocFileFolder() {

@Override
public String toVarName(String name) {
return toVarName(name, "n");
}

private String toVarName(String name, String numberPrefix) {
// replace - with _ e.g. created-at => created_at
name = name.replace("-", "_");

Expand All @@ -378,7 +380,7 @@ public String toVarName(String name) {
name = camelize(name, true);

if (name.matches("^\\d.*")) {
name = "n" + name;
name = numberPrefix + name;
}

if (isReservedWord(name) || importMapping().containsKey(name)) {
Expand Down Expand Up @@ -505,78 +507,35 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
List<Object> models = (List<Object>) objs.get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
boolean succes = buildEnumFromVendorExtension(cm) ||
buildEnumFromValues(cm);
for (CodegenProperty var : cm.vars) {
updateCodegenPropertyEnum(var);
}
}
return objs;
}

/**
* Builds the set of enum members from their declared value.
*
* @return {@code true} if the enum was built
*/
private boolean buildEnumFromValues(CodegenModel cm) {
if (!cm.isEnum || cm.allowableValues == null) {
return false;
}
Map<String, Object> allowableValues = cm.allowableValues;
List<Object> values = (List<Object>) allowableValues.get("values");
List<Map<String, Object>> enumVars = buildEnumVars(values, cm.dataType);
cm.allowableValues.put("enumVars", enumVars);
return true;
}

/**
* Builds the set of enum members from a vendor extension.
*
* @return {@code true} if the enum was built
*/
private boolean buildEnumFromVendorExtension(CodegenModel cm) {
if (!cm.isEnum || cm.allowableValues == null ||
!useEnumExtension ||
!cm.vendorExtensions.containsKey("x-enum-values")) {
return false;
}
Object extension = cm.vendorExtensions.get("x-enum-values");
List<Map<String, Object>> values = (List<Map<String, Object>>) extension;
List<Map<String, String>> enumVars = new ArrayList<>();
for (Map<String, Object> value : values) {
Map<String, String> enumVar = new HashMap<>();
String name = camelize((String) value.get("identifier"), true);
if (isReservedWord(name)) {
name = escapeReservedWord(name);
}
enumVar.put("name", name);
enumVar.put("value", toEnumValue(value.get("numericValue").toString(), cm.dataType));
if (value.containsKey("description")) {
enumVar.put("description", value.get("description").toString());
protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions, String dataType) {
if (vendorExtensions != null && useEnumExtension && vendorExtensions.containsKey("x-enum-values")) {
// Use the x-enum-values extension for this enum
// Existing enumVars added by the default handling need to be removed first
enumVars.clear();

Object extension = vendorExtensions.get("x-enum-values");
List<Map<String, Object>> values = (List<Map<String, Object>>) extension;
for (Map<String, Object> value : values) {
Map<String, Object> enumVar = new HashMap<>();
enumVar.put("name", toEnumVarName((String) value.get("identifier"), dataType));
enumVar.put("value", toEnumValue(value.get("numericValue").toString(), dataType));
enumVar.put("isString", isDataTypeString(dataType));
if (value.containsKey("description")) {
enumVar.put("description", value.get("description").toString());
}
enumVars.add(enumVar);
}
enumVars.add(enumVar);
} else {
super.updateEnumVarsWithExtensions(enumVars, vendorExtensions, dataType);
}
cm.allowableValues.put("enumVars", enumVars);
return true;
}

@Override
public String toEnumVarName(String value, String datatype) {
if (value.length() == 0) {
return "empty";
}
String var = value.replaceAll("\\W+", "_");
if ("number".equalsIgnoreCase(datatype) ||
"int".equalsIgnoreCase(datatype)) {
var = "Number" + var;
}
return escapeReservedWord(camelize(var, true));
return toVarName(value, "number");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
addImport(codegenModel, codegenModel.additionalPropertiesType);
}

@Override
public String toEnumVarName(String name, String datatype) {
if (name.length() == 0) {
return "empty";
}
if ("number".equalsIgnoreCase(datatype) || "int".equalsIgnoreCase(datatype)) {
name = "Number" + name;
}
name = camelize(name, true);
// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}
return name;
}

@Override
public void processOpts() {
defaultProcessOpts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
if (referencedSchema != null) {
extensions = referencedSchema.getExtensions();
}
updateEnumVarsWithExtensions(enumVars, extensions);
updateEnumVarsWithExtensions(enumVars, extensions, dataType);
allowableValues.put("enumVars", enumVars);
// overwriting defaultValue omitted from here
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class {{{classname}}} {

{{#allowableValues}}
{{#enumVars}}
static const {{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}} = {{{classname}}}._({{{value}}});
static const {{{name}}} = {{{classname}}}._({{{value}}});
{{/enumVars}}
{{/allowableValues}}

/// List of all possible values in this [enum][{{{classname}}}].
static const values = <{{{classname}}}>[
{{#allowableValues}}
{{#enumVars}}
{{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}},
{{{name}}},
{{/enumVars}}
{{/allowableValues}}
];
Expand Down Expand Up @@ -71,7 +71,7 @@ class {{{classname}}}TypeTransformer {
switch (data) {
{{#allowableValues}}
{{#enumVars}}
case {{{value}}}: return {{{classname}}}.{{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}};
case {{{value}}}: return {{{classname}}}.{{{name}}};
{{/enumVars}}
{{/allowableValues}}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class {{{classname}}}{{{enumName}}} {

{{#allowableValues}}
{{#enumVars}}
static const {{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}} = {{{classname}}}{{{enumName}}}._({{{value}}});
static const {{{name}}} = {{{classname}}}{{{enumName}}}._({{{value}}});
{{/enumVars}}
{{/allowableValues}}

/// List of all possible values in this [enum][{{{classname}}}{{{enumName}}}].
static const values = <{{{classname}}}{{{enumName}}}>[
{{#allowableValues}}
{{#enumVars}}
{{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}},
{{{name}}},
{{/enumVars}}
{{/allowableValues}}
];
Expand Down Expand Up @@ -71,7 +71,7 @@ class {{{classname}}}{{{enumName}}}TypeTransformer {
switch (data) {
{{#allowableValues}}
{{#enumVars}}
case {{{value}}}: return {{{classname}}}{{{enumName}}}.{{#lambda.lowercase}}{{{name}}}{{/lambda.lowercase}};
case {{{value}}}: return {{{classname}}}{{{enumName}}}.{{{name}}};
{{/enumVars}}
{{/allowableValues}}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public void updateCodegenPropertyEnum() {
}

@Test
public void updateCodegenPropertyEnumWithExtention() {
public void updateCodegenPropertyEnumWithExtension() {
{
CodegenProperty enumProperty = codegenPropertyWithXEnumVarName(Arrays.asList("dog", "cat"), Arrays.asList("DOGVAR", "CATVAR"));
(new DefaultCodegen()).updateCodegenPropertyEnum(enumProperty);
Expand Down
Loading

0 comments on commit 0e4f449

Please sign in to comment.