Skip to content

Commit

Permalink
Merge pull request #806 from swagger-api/codegen-enum-values-issue
Browse files Browse the repository at this point in the history
fixed java, javascript and kotlin enumeration issue
  • Loading branch information
HugoMario authored Nov 17, 2020
2 parents a0b1a60 + e969141 commit f2adecb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ public void processModelEnums(Map<String, Object> objs) {
}
}

public boolean isPrimivite(String datatype) {
return "number".equalsIgnoreCase(datatype)
|| "integer".equalsIgnoreCase(datatype)
|| "boolean".equalsIgnoreCase(datatype);
}

/**
* update codegen property enum with proper naming convention
* and handling of numbers, special characters
Expand Down Expand Up @@ -1465,6 +1471,9 @@ else if (schema instanceof ComposedSchema) {
// comment out below as allowableValues is not set in post processing model enum
codegenModel.allowableValues = new HashMap<String, Object>();
codegenModel.allowableValues.put("values", schema.getEnum());
if (codegenModel.dataType.equals("BigDecimal")) {
addImport(codegenModel, "BigDecimal");
}
}
addVars(codegenModel, schema.getProperties(), schema.getRequired());
}
Expand All @@ -1474,6 +1483,15 @@ else if (schema instanceof ComposedSchema) {
postProcessModelProperty(codegenModel, prop);
}
}

System.out.println(codegenModel.name);
System.out.println(codegenModel.dataType);
System.out.println(codegenModel.getIsInteger());
System.out.println(codegenModel.getIsNumber());
System.out.println(codegenModel.getIsBoolean());
System.out.println("--------");


return codegenModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,7 @@ public String toEnumVarName(String value, String datatype) {
}

// number
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
"Float".equals(datatype) || "Double".equals(datatype)) {
if ("Integer".equals(datatype) || "Long".equals(datatype) || "Float".equals(datatype) || "Double".equals(datatype) || "BigDecimal".equals(datatype)) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
Expand All @@ -1281,14 +1280,17 @@ public String toEnumValue(String value, String datatype) {
if (value == null) {
return null;
}
if ("Integer".equals(datatype) || "Double".equals(datatype)) {
System.out.println("|||||||||||||||||||||||||||| datatype: " + datatype);
if ("Integer".equals(datatype) || "Double".equals(datatype) || "Boolean".equals(datatype)) {
return value;
} else if ("Long".equals(datatype)) {
// add l to number, e.g. 2048 => 2048l
return value + "l";
} else if ("Float".equals(datatype)) {
// add f to number, e.g. 3.14 => 3.14f
return value + "f";
} else if ("BigDecimal".equals(datatype)) {
return "new BigDecimal(" + escapeText(value) + ")";
} else {
return "\"" + escapeText(value) + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ public String toEnumVarName(String value, String datatype) {

@Override
public String toEnumValue(String value, String datatype) {
if ("Integer".equals(datatype) || "Number".equals(datatype)) {
if ("Integer".equals(datatype) || "Number".equals(datatype) || "Boolean".equals(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,25 @@ public String toEnumVarName(String value, String datatype) {
return modified;
}

@Override
public String toEnumValue(String value, String datatype) {
if (isPrimivite(datatype)) {
return value;
}
return super.toEnumValue(value, datatype);
}

@Override
public boolean isPrimivite(String datatype) {
return "kotlin.Byte".equalsIgnoreCase(datatype)
|| "kotlin.Short".equalsIgnoreCase(datatype)
|| "kotlin.Int".equalsIgnoreCase(datatype)
|| "kotlin.Long".equalsIgnoreCase(datatype)
|| "kotlin.Float".equalsIgnoreCase(datatype)
|| "kotlin.Double".equalsIgnoreCase(datatype)
|| "kotlin.Boolean".equalsIgnoreCase(datatype);
}

@Override
public String toInstantiationType(Schema p) {
if (p instanceof ArraySchema) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/Java/modelEnum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = jsonReader.{{#is this 'integer'}}nextInt(){{/is}}{{#isNot this 'integer'}}next{{{dataType}}}(){{/isNot}};
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/Java/modelInnerEnum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{datatype}}} value = jsonReader.{{#is ../this 'integer'}}nextInt(){{/is}}{{#isNot ../this 'integer'}}next{{{datatype}}}(){{/isNot}};
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}{{/gson}}
Expand Down

0 comments on commit f2adecb

Please sign in to comment.