mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 06:30:52 +00:00
* 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:
parent
8f738a9b41
commit
b05df5d3ef
@ -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.media.StringSchema;
|
||||||
import io.swagger.v3.oas.models.servers.Server;
|
import io.swagger.v3.oas.models.servers.Server;
|
||||||
import io.swagger.v3.parser.util.SchemaTypeUtil;
|
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.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -758,9 +760,9 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toDefaultValue(Schema p) {
|
public String toDefaultValue(Schema schema) {
|
||||||
p = ModelUtils.getReferencedSchema(this.openAPI, p);
|
schema = ModelUtils.getReferencedSchema(this.openAPI, schema);
|
||||||
if (ModelUtils.isArraySchema(p)) {
|
if (ModelUtils.isArraySchema(schema)) {
|
||||||
final String pattern;
|
final String pattern;
|
||||||
if (fullJavaUtil) {
|
if (fullJavaUtil) {
|
||||||
pattern = "new java.util.ArrayList<%s>()";
|
pattern = "new java.util.ArrayList<%s>()";
|
||||||
@ -768,7 +770,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
pattern = "new ArrayList<%s>()";
|
pattern = "new ArrayList<%s>()";
|
||||||
}
|
}
|
||||||
|
|
||||||
Schema<?> items = getSchemaItems((ArraySchema) p);
|
Schema<?> items = getSchemaItems((ArraySchema) schema);
|
||||||
|
|
||||||
String typeDeclaration = getTypeDeclaration(items);
|
String typeDeclaration = getTypeDeclaration(items);
|
||||||
Object java8obj = additionalProperties.get("java8");
|
Object java8obj = additionalProperties.get("java8");
|
||||||
@ -780,18 +782,18 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
}
|
}
|
||||||
|
|
||||||
return String.format(Locale.ROOT, pattern, typeDeclaration);
|
return String.format(Locale.ROOT, pattern, typeDeclaration);
|
||||||
} else if (ModelUtils.isMapSchema(p)) {
|
} else if (ModelUtils.isMapSchema(schema)) {
|
||||||
final String pattern;
|
final String pattern;
|
||||||
if (fullJavaUtil) {
|
if (fullJavaUtil) {
|
||||||
pattern = "new java.util.HashMap<%s>()";
|
pattern = "new java.util.HashMap<%s>()";
|
||||||
} else {
|
} else {
|
||||||
pattern = "new HashMap<%s>()";
|
pattern = "new HashMap<%s>()";
|
||||||
}
|
}
|
||||||
if (ModelUtils.getAdditionalProperties(p) == null) {
|
if (ModelUtils.getAdditionalProperties(schema) == null) {
|
||||||
return 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");
|
Object java8obj = additionalProperties.get("java8");
|
||||||
if (java8obj != null) {
|
if (java8obj != null) {
|
||||||
Boolean java8 = Boolean.valueOf(java8obj.toString());
|
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);
|
return String.format(Locale.ROOT, pattern, typeDeclaration);
|
||||||
} else if (ModelUtils.isIntegerSchema(p)) {
|
} else if (ModelUtils.isIntegerSchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(p.getFormat())) {
|
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) {
|
||||||
return p.getDefault().toString() + "l";
|
return schema.getDefault().toString() + "l";
|
||||||
} else {
|
} else {
|
||||||
return p.getDefault().toString();
|
return schema.getDefault().toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} else if (ModelUtils.isNumberSchema(p)) {
|
} else if (ModelUtils.isNumberSchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
if (SchemaTypeUtil.FLOAT_FORMAT.equals(p.getFormat())) {
|
if (SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) {
|
||||||
return p.getDefault().toString() + "f";
|
return schema.getDefault().toString() + "f";
|
||||||
} else {
|
} else {
|
||||||
return p.getDefault().toString() + "d";
|
return schema.getDefault().toString() + "d";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} else if (ModelUtils.isBooleanSchema(p)) {
|
} else if (ModelUtils.isBooleanSchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
return p.getDefault().toString();
|
return schema.getDefault().toString();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} else if (ModelUtils.isURISchema(p)) {
|
} else if (ModelUtils.isURISchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
return "URI.create(\"" + escapeText((String) p.getDefault()) + "\")";
|
return "URI.create(\"" + escapeText((String) schema.getDefault()) + "\")";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} else if (ModelUtils.isStringSchema(p)) {
|
} else if (ModelUtils.isStringSchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
String _default = (String) p.getDefault();
|
String _default;
|
||||||
if (p.getEnum() == null) {
|
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) + "\"";
|
return "\"" + escapeText(_default) + "\"";
|
||||||
} else {
|
} else {
|
||||||
// convert to enum var name later in postProcessModels
|
// convert to enum var name later in postProcessModels
|
||||||
@ -840,14 +850,14 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} else if (ModelUtils.isObjectSchema(p)) {
|
} else if (ModelUtils.isObjectSchema(schema)) {
|
||||||
if (p.getDefault() != null) {
|
if (schema.getDefault() != null) {
|
||||||
return super.toDefaultValue(p);
|
return super.toDefaultValue(schema);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.toDefaultValue(p);
|
return super.toDefaultValue(schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,6 +21,10 @@ import io.swagger.v3.oas.models.Components;
|
|||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import io.swagger.v3.oas.models.media.*;
|
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.CodegenConstants;
|
||||||
import org.openapitools.codegen.CodegenType;
|
import org.openapitools.codegen.CodegenType;
|
||||||
import org.openapitools.codegen.TestUtils;
|
import org.openapitools.codegen.TestUtils;
|
||||||
@ -411,6 +415,12 @@ public class AbstractJavaCodegenTest {
|
|||||||
Schema<?> schema = createObjectSchemaWithMinItems();
|
Schema<?> schema = createObjectSchemaWithMinItems();
|
||||||
String defaultValue = codegen.toDefaultValue(schema);
|
String defaultValue = codegen.toDefaultValue(schema);
|
||||||
Assert.assertNull(defaultValue);
|
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
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user