[Java] Error generating java due to a default for a date #5086 (#5333)

* Fix OpenAPITools#5086 handling date examples in openapi 3.0

* Fix OpenAPITools#5086
created test for handling date value
renamed variable p to schema
created field birthday in test yaml

* Fix OpenAPITools#5086
reverted birthday, because breaking tests

* Fix OpenAPITools#5086
create String in ISO format for date default. That can be converted to LocalDate
fixed Test
This commit is contained in:
Alexej 2020-02-25 04:24:14 +01:00 committed by GitHub
parent 8f738a9b41
commit b05df5d3ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 32 deletions

View File

@ -26,6 +26,8 @@ import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import java.time.LocalDate;
import java.time.ZoneId;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@ -758,9 +760,9 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
@Override
public String toDefaultValue(Schema p) {
p = ModelUtils.getReferencedSchema(this.openAPI, p);
if (ModelUtils.isArraySchema(p)) {
public String toDefaultValue(Schema schema) {
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
if (ModelUtils.isArraySchema(schema)) {
final String pattern;
if (fullJavaUtil) {
pattern = "new java.util.ArrayList<%s>()";
@ -768,7 +770,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
pattern = "new ArrayList<%s>()";
}
Schema<?> items = getSchemaItems((ArraySchema) p);
Schema<?> items = getSchemaItems((ArraySchema) schema);
String typeDeclaration = getTypeDeclaration(items);
Object java8obj = additionalProperties.get("java8");
@ -780,18 +782,18 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
return String.format(Locale.ROOT, pattern, typeDeclaration);
} else if (ModelUtils.isMapSchema(p)) {
} else if (ModelUtils.isMapSchema(schema)) {
final String pattern;
if (fullJavaUtil) {
pattern = "new java.util.HashMap<%s>()";
} else {
pattern = "new HashMap<%s>()";
}
if (ModelUtils.getAdditionalProperties(p) == null) {
if (ModelUtils.getAdditionalProperties(schema) == null) {
return null;
}
String typeDeclaration = String.format(Locale.ROOT, "String, %s", getTypeDeclaration(ModelUtils.getAdditionalProperties(p)));
String typeDeclaration = String.format(Locale.ROOT, "String, %s", getTypeDeclaration(ModelUtils.getAdditionalProperties(schema)));
Object java8obj = additionalProperties.get("java8");
if (java8obj != null) {
Boolean java8 = Boolean.valueOf(java8obj.toString());
@ -801,38 +803,46 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
return String.format(Locale.ROOT, pattern, typeDeclaration);
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) {
return p.getDefault().toString() + "l";
} else if (ModelUtils.isIntegerSchema(schema)) {
if (schema.getDefault() != null) {
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) {
return schema.getDefault().toString() + "l";
} else {
return p.getDefault().toString();
return schema.getDefault().toString();
}
}
return null;
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) {
return p.getDefault().toString() + "f";
} else if (ModelUtils.isNumberSchema(schema)) {
if (schema.getDefault() != null) {
if (SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) {
return schema.getDefault().toString() + "f";
} else {
return p.getDefault().toString() + "d";
return schema.getDefault().toString() + "d";
}
}
return null;
} else if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (ModelUtils.isBooleanSchema(schema)) {
if (schema.getDefault() != null) {
return schema.getDefault().toString();
}
return null;
} else if (ModelUtils.isURISchema(p)) {
if (p.getDefault() != null) {
return "URI.create(\"" + escapeText((String) p.getDefault()) + "\")";
} else if (ModelUtils.isURISchema(schema)) {
if (schema.getDefault() != null) {
return "URI.create(\"" + escapeText((String) schema.getDefault()) + "\")";
}
return null;
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
String _default = (String) p.getDefault();
if (p.getEnum() == null) {
} else if (ModelUtils.isStringSchema(schema)) {
if (schema.getDefault() != null) {
String _default;
if (schema.getDefault() instanceof Date){
Date date = (Date) schema.getDefault();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return String.format(Locale.ROOT, localDate.toString(), "");
}
else{
_default = (String) schema.getDefault();
}
if (schema.getEnum() == null) {
return "\"" + escapeText(_default) + "\"";
} else {
// convert to enum var name later in postProcessModels
@ -840,14 +850,14 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
}
return null;
} else if (ModelUtils.isObjectSchema(p)) {
if (p.getDefault() != null) {
return super.toDefaultValue(p);
} else if (ModelUtils.isObjectSchema(schema)) {
if (schema.getDefault() != null) {
return super.toDefaultValue(schema);
}
return null;
}
return super.toDefaultValue(p);
return super.toDefaultValue(schema);
}
@Override

View File

@ -21,6 +21,10 @@ import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.*;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.TestUtils;
@ -411,6 +415,12 @@ public class AbstractJavaCodegenTest {
Schema<?> schema = createObjectSchemaWithMinItems();
String defaultValue = codegen.toDefaultValue(schema);
Assert.assertNull(defaultValue);
DateSchema dateSchema = new DateSchema();
LocalDate defaultLocalDate = LocalDate.of(2019,2,15);
Date date = Date.from(defaultLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
dateSchema.setDefault(date);
defaultValue = codegen.toDefaultValue(dateSchema);
Assert.assertEquals(defaultLocalDate, LocalDate.parse(defaultValue));
}
@Test