fix java incompatible types error for number (#8018)

This commit is contained in:
bgong-mdsol 2020-11-25 04:54:49 -05:00 committed by GitHub
parent 8cfc9b015a
commit ca6c63f7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -883,8 +883,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
if (schema.getDefault() != null) {
if (SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) {
return schema.getDefault().toString() + "f";
} else {
} else if (SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat())) {
return schema.getDefault().toString() + "d";
} else {
return "new BigDecimal(\"" + schema.getDefault().toString() + "\")";
}
}
return null;

View File

@ -448,6 +448,18 @@ public class AbstractJavaCodegenTest {
dateSchema.setDefault(date);
defaultValue = codegen.toDefaultValue(dateSchema);
Assert.assertEquals(defaultLocalDate, LocalDate.parse(defaultValue));
// Test default value for number without format
NumberSchema numberSchema = new NumberSchema();
Double doubleValue = 100.0;
numberSchema.setDefault(doubleValue);
defaultValue = codegen.toDefaultValue(numberSchema);
Assert.assertEquals(defaultValue, "new BigDecimal(\"" + doubleValue.toString() + "\")");
// Test default value for number with format set to double
numberSchema.setFormat("double");
defaultValue = codegen.toDefaultValue(numberSchema);
Assert.assertEquals(defaultValue, doubleValue + "d");
}
@Test