Skip to content

Commit a0dd739

Browse files
jminiwing328
authored andcommitted
[java] Support for number enum (#3328)
* [java] Support for number enum * [core] add isLong, isNumber, isNumeric, isFloat, isDouble to CodegenModel * [java-gson] fix Enum TypeAdapter in BigDecimal case.
1 parent 3f7725c commit a0dd739

File tree

6 files changed

+195
-10
lines changed

6 files changed

+195
-10
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class CodegenModel {
4646
public String defaultValue;
4747
public String arrayModelType;
4848
public boolean isAlias; // Is this effectively an alias of another simple type
49-
public boolean isString, isInteger;
49+
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble;
5050
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
5151
public List<CodegenProperty> allVars = new ArrayList<CodegenProperty>(); // all properties (with parent's properties)
5252
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>(); // a list of required properties
@@ -96,7 +96,12 @@ public String toString() {
9696
.append("arrayModelType", arrayModelType)
9797
.append("isAlias", isAlias)
9898
.append("isString", isString)
99+
.append("isNumeric", isNumeric)
99100
.append("isInteger", isInteger)
101+
.append("isLong", isLong)
102+
.append("isNumber", isNumber)
103+
.append("isFloat", isFloat)
104+
.append("isDouble", isDouble)
100105
.append("vars", vars)
101106
.append("requiredVars", requiredVars)
102107
.append("optionalVars", optionalVars)

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,14 +1885,27 @@ public CodegenModel fromModel(String name, Schema schema) {
18851885
addAdditionPropertiesToCodeGenModel(m, schema);
18861886
m.isMapModel = true;
18871887
}
1888-
if (ModelUtils.isIntegerSchema(schema)) { // integer type
1889-
if (!ModelUtils.isLongSchema(schema)) { // long type is not integer
1888+
else if (ModelUtils.isIntegerSchema(schema)) { // integer type
1889+
m.isNumeric = Boolean.TRUE;
1890+
if (ModelUtils.isLongSchema(schema)) { // int64/long format
1891+
m.isLong = Boolean.TRUE;
1892+
} else { // int32 format
18901893
m.isInteger = Boolean.TRUE;
18911894
}
18921895
}
1893-
if (ModelUtils.isStringSchema(schema)) {
1896+
else if (ModelUtils.isStringSchema(schema)) {
18941897
m.isString = Boolean.TRUE;
18951898
}
1899+
else if (ModelUtils.isNumberSchema(schema)) {
1900+
m.isNumeric = Boolean.TRUE;
1901+
if (ModelUtils.isFloatSchema(schema)) { // float
1902+
m.isFloat = Boolean.TRUE;
1903+
} else if (ModelUtils.isDoubleSchema(schema)) { // double
1904+
m.isDouble = Boolean.TRUE;
1905+
} else { // type is number and without format
1906+
m.isNumber = Boolean.TRUE;
1907+
}
1908+
}
18961909

18971910
// passing null to allProperties and allRequired as there's no parent
18981911
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
@@ -2059,7 +2072,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
20592072
String type = getSchemaType(p);
20602073
if (ModelUtils.isIntegerSchema(p)) { // integer type
20612074
property.isNumeric = Boolean.TRUE;
2062-
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) { // int64/long format
2075+
if (ModelUtils.isLongSchema(p)) { // int64/long format
20632076
property.isLong = Boolean.TRUE;
20642077
} else { // int32 format
20652078
property.isInteger = Boolean.TRUE;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ public void processOpts() {
417417
// imports for pojos
418418
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
419419
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
420+
importMapping.put("BigDecimal", "java.math.BigDecimal");
420421
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
421422
importMapping.put("JsonSubTypes", "com.fasterxml.jackson.annotation.JsonSubTypes");
422423
importMapping.put("JsonTypeInfo", "com.fasterxml.jackson.annotation.JsonTypeInfo");
@@ -944,6 +945,9 @@ public CodegenModel fromModel(String name, Schema model) {
944945
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
945946
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
946947
}
948+
if ("BigDecimal".equals(codegenModel.dataType)) {
949+
codegenModel.imports.add("BigDecimal");
950+
}
947951
return codegenModel;
948952
}
949953

@@ -1107,7 +1111,7 @@ public String toEnumVarName(String value, String datatype) {
11071111

11081112
// number
11091113
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
1110-
"Float".equals(datatype) || "Double".equals(datatype)) {
1114+
"Float".equals(datatype) || "Double".equals(datatype) || "BigDecimal".equals(datatype)) {
11111115
String varName = "NUMBER_" + value;
11121116
varName = varName.replaceAll("-", "MINUS_");
11131117
varName = varName.replaceAll("\\+", "PLUS_");
@@ -1134,6 +1138,9 @@ public String toEnumValue(String value, String datatype) {
11341138
} else if ("Float".equals(datatype)) {
11351139
// add f to number, e.g. 3.14 => 3.14f
11361140
return value + "f";
1141+
} else if ("BigDecimal".equals(datatype)) {
1142+
// use BigDecimal String constructor
1143+
return "new BigDecimal(\"" + value + "\")";
11371144
} else {
11381145
return "\"" + escapeText(value) + "\"";
11391146
}

modules/openapi-generator/src/main/resources/Java/modelEnum.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
6565

6666
@Override
6767
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
68-
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
69-
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(value);
68+
{{^isNumber}}{{{dataType}}}{{/isNumber}}{{#isNumber}}String{{/isNumber}} value = jsonReader.{{#isNumber}}nextString(){{/isNumber}}{{#isInteger}}nextInt(){{/isInteger}}{{^isNumber}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
69+
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue({{#isNumber}}new BigDecimal({{/isNumber}}value{{#isNumber}}){{/isNumber}});
7070
}
7171
}
7272
{{/gson}}

modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656

5757
@Override
5858
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
59-
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
60-
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(value);
59+
{{^isNumber}}{{{dataType}}}{{/isNumber}}{{#isNumber}}String{{/isNumber}} value = jsonReader.{{#isNumber}}nextString(){{/isNumber}}{{#isInteger}}nextInt(){{/isInteger}}{{^isNumber}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
60+
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue({{#isNumber}}new BigDecimal({{/isNumber}}value{{#isNumber}}){{/isNumber}});
6161
}
6262
}
6363
{{/gson}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,166 @@ public void testNullableProperty() {
578578
Assert.assertTrue(property.isNullable);
579579
}
580580

581+
@Test
582+
public void integerSchemaPropertyAndModelTest() {
583+
OpenAPI openAPI = TestUtils.createOpenAPI();
584+
final Schema schema = new IntegerSchema().format("int32");
585+
final DefaultCodegen codegen = new DefaultCodegen();
586+
codegen.setOpenAPI(openAPI);
587+
588+
//Property:
589+
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
590+
Assert.assertEquals(cp.baseType, "integer");
591+
Assert.assertEquals(cp.baseName, "someProperty");
592+
Assert.assertFalse(cp.isString);
593+
Assert.assertTrue(cp.isInteger);
594+
Assert.assertFalse(cp.isLong);
595+
Assert.assertFalse(cp.isNumber);
596+
Assert.assertTrue(cp.isNumeric);
597+
Assert.assertFalse(cp.isFloat);
598+
Assert.assertFalse(cp.isDouble);
599+
600+
//Model:
601+
final CodegenModel cm = codegen.fromModel("someModel", schema);
602+
Assert.assertEquals(cm.dataType, "integer");
603+
Assert.assertEquals(cm.name, "someModel");
604+
Assert.assertFalse(cm.isString);
605+
Assert.assertTrue(cm.isInteger);
606+
Assert.assertFalse(cm.isLong);
607+
Assert.assertFalse(cm.isNumber);
608+
Assert.assertTrue(cm.isNumeric);
609+
Assert.assertFalse(cm.isFloat);
610+
Assert.assertFalse(cm.isDouble);
611+
}
612+
613+
@Test
614+
public void longSchemaPropertyAndModelTest() {
615+
OpenAPI openAPI = TestUtils.createOpenAPI();
616+
final Schema schema = new IntegerSchema().format("int64");
617+
final DefaultCodegen codegen = new DefaultCodegen();
618+
codegen.setOpenAPI(openAPI);
619+
620+
//Property:
621+
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
622+
Assert.assertEquals(cp.baseType, "long");
623+
Assert.assertEquals(cp.baseName, "someProperty");
624+
Assert.assertFalse(cp.isString);
625+
Assert.assertFalse(cp.isInteger);
626+
Assert.assertTrue(cp.isLong);
627+
Assert.assertFalse(cp.isNumber);
628+
Assert.assertTrue(cp.isNumeric);
629+
Assert.assertFalse(cp.isFloat);
630+
Assert.assertFalse(cp.isDouble);
631+
632+
//Model:
633+
final CodegenModel cm = codegen.fromModel("someModel", schema);
634+
Assert.assertEquals(cm.dataType, "long");
635+
Assert.assertEquals(cm.name, "someModel");
636+
Assert.assertFalse(cm.isString);
637+
Assert.assertFalse(cm.isInteger);
638+
Assert.assertTrue(cm.isLong);
639+
Assert.assertFalse(cm.isNumber);
640+
Assert.assertTrue(cm.isNumeric);
641+
Assert.assertFalse(cm.isFloat);
642+
Assert.assertFalse(cm.isDouble);
643+
}
644+
645+
@Test
646+
public void numberSchemaPropertyAndModelTest() {
647+
OpenAPI openAPI = TestUtils.createOpenAPI();
648+
final Schema schema = new NumberSchema();
649+
final DefaultCodegen codegen = new DefaultCodegen();
650+
codegen.setOpenAPI(openAPI);
651+
652+
//Property:
653+
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
654+
Assert.assertEquals(cp.baseType, "number");
655+
Assert.assertEquals(cp.baseName, "someProperty");
656+
Assert.assertFalse(cp.isString);
657+
Assert.assertFalse(cp.isInteger);
658+
Assert.assertFalse(cp.isLong);
659+
Assert.assertTrue(cp.isNumber);
660+
Assert.assertTrue(cp.isNumeric);
661+
Assert.assertFalse(cp.isFloat);
662+
Assert.assertFalse(cp.isDouble);
663+
664+
//Model:
665+
final CodegenModel cm = codegen.fromModel("someModel", schema);
666+
Assert.assertEquals(cm.dataType, "number");
667+
Assert.assertEquals(cm.name, "someModel");
668+
Assert.assertFalse(cm.isString);
669+
Assert.assertFalse(cm.isInteger);
670+
Assert.assertFalse(cm.isLong);
671+
Assert.assertTrue(cm.isNumber);
672+
Assert.assertTrue(cm.isNumeric);
673+
Assert.assertFalse(cm.isFloat);
674+
Assert.assertFalse(cm.isDouble);
675+
}
676+
677+
@Test
678+
public void numberFloatSchemaPropertyAndModelTest() {
679+
OpenAPI openAPI = TestUtils.createOpenAPI();
680+
final Schema schema = new NumberSchema().format("float");
681+
final DefaultCodegen codegen = new DefaultCodegen();
682+
codegen.setOpenAPI(openAPI);
683+
684+
//Property:
685+
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
686+
Assert.assertEquals(cp.baseType, "float");
687+
Assert.assertEquals(cp.baseName, "someProperty");
688+
Assert.assertFalse(cp.isString);
689+
Assert.assertFalse(cp.isInteger);
690+
Assert.assertFalse(cp.isLong);
691+
Assert.assertFalse(cp.isNumber);
692+
Assert.assertTrue(cp.isNumeric);
693+
Assert.assertTrue(cp.isFloat);
694+
Assert.assertFalse(cp.isDouble);
695+
696+
//Model:
697+
final CodegenModel cm = codegen.fromModel("someModel", schema);
698+
Assert.assertEquals(cm.dataType, "float");
699+
Assert.assertEquals(cm.name, "someModel");
700+
Assert.assertFalse(cm.isString);
701+
Assert.assertFalse(cm.isInteger);
702+
Assert.assertFalse(cm.isLong);
703+
Assert.assertFalse(cm.isNumber);
704+
Assert.assertTrue(cm.isNumeric);
705+
Assert.assertTrue(cm.isFloat);
706+
Assert.assertFalse(cm.isDouble);
707+
}
708+
709+
@Test
710+
public void numberDoubleSchemaPropertyAndModelTest() {
711+
OpenAPI openAPI = TestUtils.createOpenAPI();
712+
final Schema schema = new NumberSchema().format("double");
713+
final DefaultCodegen codegen = new DefaultCodegen();
714+
codegen.setOpenAPI(openAPI);
715+
716+
//Property:
717+
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
718+
Assert.assertEquals(cp.baseType, "double");
719+
Assert.assertEquals(cp.baseName, "someProperty");
720+
Assert.assertFalse(cp.isString);
721+
Assert.assertFalse(cp.isInteger);
722+
Assert.assertFalse(cp.isLong);
723+
Assert.assertFalse(cp.isNumber);
724+
Assert.assertTrue(cp.isNumeric);
725+
Assert.assertFalse(cp.isFloat);
726+
Assert.assertTrue(cp.isDouble);
727+
728+
//Model:
729+
final CodegenModel cm = codegen.fromModel("someModel", schema);
730+
Assert.assertEquals(cm.dataType, "double");
731+
Assert.assertEquals(cm.name, "someModel");
732+
Assert.assertFalse(cm.isString);
733+
Assert.assertFalse(cm.isInteger);
734+
Assert.assertFalse(cm.isLong);
735+
Assert.assertFalse(cm.isNumber);
736+
Assert.assertTrue(cm.isNumeric);
737+
Assert.assertFalse(cm.isFloat);
738+
Assert.assertTrue(cm.isDouble);
739+
}
740+
581741
private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
582742
CodegenDiscriminator test = new CodegenDiscriminator();
583743
test.setPropertyName("DollarUnderscoretype");

0 commit comments

Comments
 (0)