Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[java] Support for number enum #3328

Merged
merged 3 commits into from
Jul 25, 2019
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 @@ -46,7 +46,7 @@ public class CodegenModel {
public String defaultValue;
public String arrayModelType;
public boolean isAlias; // Is this effectively an alias of another simple type
public boolean isString, isInteger;
public boolean isString, isInteger, isLong, isNumber, isNumeric, isFloat, isDouble;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>(); // all properties (without parent's properties)
public List<CodegenProperty> allVars = new ArrayList<CodegenProperty>(); // all properties (with parent's properties)
public List<CodegenProperty> requiredVars = new ArrayList<CodegenProperty>(); // a list of required properties
Expand Down Expand Up @@ -96,7 +96,12 @@ public String toString() {
.append("arrayModelType", arrayModelType)
.append("isAlias", isAlias)
.append("isString", isString)
.append("isNumeric", isNumeric)
.append("isInteger", isInteger)
.append("isLong", isLong)
.append("isNumber", isNumber)
.append("isFloat", isFloat)
.append("isDouble", isDouble)
.append("vars", vars)
.append("requiredVars", requiredVars)
.append("optionalVars", optionalVars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1832,14 +1832,27 @@ public CodegenModel fromModel(String name, Schema schema) {
addAdditionPropertiesToCodeGenModel(m, schema);
m.isMapModel = true;
}
if (ModelUtils.isIntegerSchema(schema)) { // integer type
if (!ModelUtils.isLongSchema(schema)) { // long type is not integer
else if (ModelUtils.isIntegerSchema(schema)) { // integer type
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isLongSchema(schema)) { // int64/long format
m.isLong = Boolean.TRUE;
} else { // int32 format
m.isInteger = Boolean.TRUE;
}
}
if (ModelUtils.isStringSchema(schema)) {
else if (ModelUtils.isStringSchema(schema)) {
m.isString = Boolean.TRUE;
}
else if (ModelUtils.isNumberSchema(schema)) {
m.isNumeric = Boolean.TRUE;
if (ModelUtils.isFloatSchema(schema)) { // float
m.isFloat = Boolean.TRUE;
} else if (ModelUtils.isDoubleSchema(schema)) { // double
m.isDouble = Boolean.TRUE;
} else { // type is number and without format
m.isNumber = Boolean.TRUE;
}
}

// passing null to allProperties and allRequired as there's no parent
addVars(m, unaliasPropertySchema(schema.getProperties()), schema.getRequired(), null, null);
Expand Down Expand Up @@ -2006,7 +2019,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
String type = getSchemaType(p);
if (ModelUtils.isIntegerSchema(p)) { // integer type
property.isNumeric = Boolean.TRUE;
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) { // int64/long format
if (ModelUtils.isLongSchema(p)) { // int64/long format
property.isLong = Boolean.TRUE;
} else { // int32 format
property.isInteger = Boolean.TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ public void processOpts() {
// imports for pojos
importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
importMapping.put("BigDecimal", "java.math.BigDecimal");
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
importMapping.put("JsonSubTypes", "com.fasterxml.jackson.annotation.JsonSubTypes");
importMapping.put("JsonTypeInfo", "com.fasterxml.jackson.annotation.JsonTypeInfo");
Expand Down Expand Up @@ -944,6 +945,9 @@ public CodegenModel fromModel(String name, Schema model) {
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
}
if ("BigDecimal".equals(codegenModel.dataType)) {
codegenModel.imports.add("BigDecimal");
}
return codegenModel;
}

Expand Down Expand Up @@ -1107,7 +1111,7 @@ public String toEnumVarName(String value, String datatype) {

// number
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
"Float".equals(datatype) || "Double".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 @@ -1134,6 +1138,9 @@ public String toEnumValue(String value, String datatype) {
} else if ("Float".equals(datatype)) {
// add f to number, e.g. 3.14 => 3.14f
return value + "f";
} else if ("BigDecimal".equals(datatype)) {
// use BigDecimal String constructor
return "new BigDecimal(\"" + value + "\")";
} else {
return "\"" + escapeText(value) + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(value);
{{^isNumber}}{{{dataType}}}{{/isNumber}}{{#isNumber}}String{{/isNumber}} value = jsonReader.{{#isNumber}}nextString(){{/isNumber}}{{#isInteger}}nextInt(){{/isInteger}}{{^isNumber}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue({{#isNumber}}new BigDecimal({{/isNumber}}value{{#isNumber}}){{/isNumber}});
}
}
{{/gson}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(value);
{{^isNumber}}{{{dataType}}}{{/isNumber}}{{#isNumber}}String{{/isNumber}} value = jsonReader.{{#isNumber}}nextString(){{/isNumber}}{{#isInteger}}nextInt(){{/isInteger}}{{^isNumber}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue({{#isNumber}}new BigDecimal({{/isNumber}}value{{#isNumber}}){{/isNumber}});
}
}
{{/gson}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,166 @@ public void testNullableProperty() {
Assert.assertTrue(property.isNullable);
}

@Test
public void integerSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema schema = new IntegerSchema().format("int32");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

//Property:
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
Assert.assertEquals(cp.baseType, "integer");
Assert.assertEquals(cp.baseName, "someProperty");
Assert.assertFalse(cp.isString);
Assert.assertTrue(cp.isInteger);
Assert.assertFalse(cp.isLong);
Assert.assertFalse(cp.isNumber);
Assert.assertTrue(cp.isNumeric);
Assert.assertFalse(cp.isFloat);
Assert.assertFalse(cp.isDouble);

//Model:
final CodegenModel cm = codegen.fromModel("someModel", schema);
Assert.assertEquals(cm.dataType, "integer");
Assert.assertEquals(cm.name, "someModel");
Assert.assertFalse(cm.isString);
Assert.assertTrue(cm.isInteger);
Assert.assertFalse(cm.isLong);
Assert.assertFalse(cm.isNumber);
Assert.assertTrue(cm.isNumeric);
Assert.assertFalse(cm.isFloat);
Assert.assertFalse(cm.isDouble);
}

@Test
public void longSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema schema = new IntegerSchema().format("int64");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

//Property:
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
Assert.assertEquals(cp.baseType, "long");
Assert.assertEquals(cp.baseName, "someProperty");
Assert.assertFalse(cp.isString);
Assert.assertFalse(cp.isInteger);
Assert.assertTrue(cp.isLong);
Assert.assertFalse(cp.isNumber);
Assert.assertTrue(cp.isNumeric);
Assert.assertFalse(cp.isFloat);
Assert.assertFalse(cp.isDouble);

//Model:
final CodegenModel cm = codegen.fromModel("someModel", schema);
Assert.assertEquals(cm.dataType, "long");
Assert.assertEquals(cm.name, "someModel");
Assert.assertFalse(cm.isString);
Assert.assertFalse(cm.isInteger);
Assert.assertTrue(cm.isLong);
Assert.assertFalse(cm.isNumber);
Assert.assertTrue(cm.isNumeric);
Assert.assertFalse(cm.isFloat);
Assert.assertFalse(cm.isDouble);
}

@Test
public void numberSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema schema = new NumberSchema();
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

//Property:
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
Assert.assertEquals(cp.baseType, "number");
Assert.assertEquals(cp.baseName, "someProperty");
Assert.assertFalse(cp.isString);
Assert.assertFalse(cp.isInteger);
Assert.assertFalse(cp.isLong);
Assert.assertTrue(cp.isNumber);
Assert.assertTrue(cp.isNumeric);
Assert.assertFalse(cp.isFloat);
Assert.assertFalse(cp.isDouble);

//Model:
final CodegenModel cm = codegen.fromModel("someModel", schema);
Assert.assertEquals(cm.dataType, "number");
Assert.assertEquals(cm.name, "someModel");
Assert.assertFalse(cm.isString);
Assert.assertFalse(cm.isInteger);
Assert.assertFalse(cm.isLong);
Assert.assertTrue(cm.isNumber);
Assert.assertTrue(cm.isNumeric);
Assert.assertFalse(cm.isFloat);
Assert.assertFalse(cm.isDouble);
}

@Test
public void numberFloatSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema schema = new NumberSchema().format("float");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

//Property:
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
Assert.assertEquals(cp.baseType, "float");
Assert.assertEquals(cp.baseName, "someProperty");
Assert.assertFalse(cp.isString);
Assert.assertFalse(cp.isInteger);
Assert.assertFalse(cp.isLong);
Assert.assertFalse(cp.isNumber);
Assert.assertTrue(cp.isNumeric);
Assert.assertTrue(cp.isFloat);
Assert.assertFalse(cp.isDouble);

//Model:
final CodegenModel cm = codegen.fromModel("someModel", schema);
Assert.assertEquals(cm.dataType, "float");
Assert.assertEquals(cm.name, "someModel");
Assert.assertFalse(cm.isString);
Assert.assertFalse(cm.isInteger);
Assert.assertFalse(cm.isLong);
Assert.assertFalse(cm.isNumber);
Assert.assertTrue(cm.isNumeric);
Assert.assertTrue(cm.isFloat);
Assert.assertFalse(cm.isDouble);
}

@Test
public void numberDoubleSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema schema = new NumberSchema().format("double");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

//Property:
final CodegenProperty cp = codegen.fromProperty("someProperty", schema);
Assert.assertEquals(cp.baseType, "double");
Assert.assertEquals(cp.baseName, "someProperty");
Assert.assertFalse(cp.isString);
Assert.assertFalse(cp.isInteger);
Assert.assertFalse(cp.isLong);
Assert.assertFalse(cp.isNumber);
Assert.assertTrue(cp.isNumeric);
Assert.assertFalse(cp.isFloat);
Assert.assertTrue(cp.isDouble);

//Model:
final CodegenModel cm = codegen.fromModel("someModel", schema);
Assert.assertEquals(cm.dataType, "double");
Assert.assertEquals(cm.name, "someModel");
Assert.assertFalse(cm.isString);
Assert.assertFalse(cm.isInteger);
Assert.assertFalse(cm.isLong);
Assert.assertFalse(cm.isNumber);
Assert.assertTrue(cm.isNumeric);
Assert.assertFalse(cm.isFloat);
Assert.assertTrue(cm.isDouble);
}

private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("DollarUnderscoretype");
Expand Down