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

fixed java, javascript and kotlin enumeration issue #806

Merged
merged 1 commit into from
Nov 17, 2020
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
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