forked from loafle/openapi-generator-original
[Java] Threetenbp dates support (#4029)
* [feign] add threetenbp support for feign clients * [okhttp] add threetenbp support for okhttp clients * [jersey] add threetenbp support for jersey clients * [retrofit2] add threetenbp support for retrofit2 clients * [spring] add threetenbp support for spring generators * add a workaround in tests for a bug in the petstore The petstore doesn't manage fractional digits of dates correctly when there are less than 3
This commit is contained in:
committed by
wing328
parent
c1e6f00242
commit
70d93883cf
@@ -47,7 +47,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
public static final String DATE_LIBRARY = "dateLibrary";
|
||||
public static final String SUPPORT_JAVA6 = "supportJava6";
|
||||
|
||||
protected String dateLibrary = "joda";
|
||||
protected String dateLibrary = "threetenbp";
|
||||
protected String invokerPackage = "io.swagger";
|
||||
protected String groupId = "io.swagger";
|
||||
protected String artifactId = "swagger-java";
|
||||
@@ -126,10 +126,11 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
|
||||
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
|
||||
Map<String, String> dateOptions = new HashMap<String, String>();
|
||||
dateOptions.put("java8", "Java 8 native");
|
||||
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+)");
|
||||
dateOptions.put("threetenbp", "Backport of JSR310 (preferred for jdk < 1.8)");
|
||||
dateOptions.put("java8-localdatetime", "Java 8 using LocalDateTime (for legacy app only)");
|
||||
dateOptions.put("joda", "Joda");
|
||||
dateOptions.put("legacy", "Legacy java.util.Date");
|
||||
dateOptions.put("joda", "Joda (for legacy app only)");
|
||||
dateOptions.put("legacy", "Legacy java.util.Date (if you really have a good reason not to use threetenbp");
|
||||
dateLibrary.setEnum(dateOptions);
|
||||
|
||||
cliOptions.add(dateLibrary);
|
||||
@@ -261,21 +262,26 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
// used later in recursive import in postProcessingModels
|
||||
importMapping.put("com.fasterxml.jackson.annotation.JsonProperty", "com.fasterxml.jackson.annotation.JsonCreator");
|
||||
|
||||
if(additionalProperties.containsKey(DATE_LIBRARY)) {
|
||||
if (additionalProperties.containsKey(DATE_LIBRARY)) {
|
||||
setDateLibrary(additionalProperties.get("dateLibrary").toString());
|
||||
additionalProperties.put(dateLibrary, "true");
|
||||
}
|
||||
|
||||
if("joda".equals(dateLibrary)) {
|
||||
if ("threetenbp".equals(dateLibrary)) {
|
||||
additionalProperties.put("threetenbp", "true");
|
||||
additionalProperties.put("jsr310", "true");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("DateTime", "OffsetDateTime");
|
||||
importMapping.put("LocalDate", "org.threeten.bp.LocalDate");
|
||||
importMapping.put("OffsetDateTime", "org.threeten.bp.OffsetDateTime");
|
||||
} else if ("joda".equals(dateLibrary)) {
|
||||
additionalProperties.put("joda", "true");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("DateTime", "DateTime");
|
||||
|
||||
importMapping.put("LocalDate", "org.joda.time.LocalDate");
|
||||
importMapping.put("DateTime", "org.joda.time.DateTime");
|
||||
}
|
||||
else if (dateLibrary.startsWith("java8")) {
|
||||
} else if (dateLibrary.startsWith("java8")) {
|
||||
additionalProperties.put("java8", "true");
|
||||
additionalProperties.put("jsr310", "true");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
importMapping.put("LocalDate", "java.time.LocalDate");
|
||||
if ("java8-localdatetime".equals(dateLibrary)) {
|
||||
@@ -285,6 +291,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
typeMapping.put("DateTime", "OffsetDateTime");
|
||||
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
|
||||
}
|
||||
} else {
|
||||
additionalProperties.put("legacyDates", "true");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -162,8 +162,11 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
|
||||
LOGGER.error("Unknown library option (-l/--library): " + getLibrary());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("jackson") ) {
|
||||
if (additionalProperties.containsKey("jackson")) {
|
||||
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache", invokerFolder, "RFC3339DateFormat.java"));
|
||||
if ("threetenbp".equals(dateLibrary)) {
|
||||
supportingFiles.add(new SupportingFile("CustomInstantDeserializer.mustache", invokerFolder, "CustomInstantDeserializer.java"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,19 @@ public class SpringCodegen extends AbstractJavaCodegen {
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
|
||||
// Process java8 option before common java ones to change the default dateLibrary to java8.
|
||||
if (additionalProperties.containsKey(JAVA_8)) {
|
||||
this.setJava8(Boolean.valueOf(additionalProperties.get(JAVA_8).toString()));
|
||||
}
|
||||
if (this.java8) {
|
||||
additionalProperties.put("javaVersion", "1.8");
|
||||
additionalProperties.put("jdk8", "true");
|
||||
if (!additionalProperties.containsKey(DATE_LIBRARY)) {
|
||||
setDateLibrary("java8");
|
||||
}
|
||||
}
|
||||
|
||||
super.processOpts();
|
||||
|
||||
// clear model and api doc template as this codegen
|
||||
@@ -112,10 +125,6 @@ public class SpringCodegen extends AbstractJavaCodegen {
|
||||
this.setSingleContentTypes(Boolean.valueOf(additionalProperties.get(SINGLE_CONTENT_TYPES).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(JAVA_8)) {
|
||||
this.setJava8(Boolean.valueOf(additionalProperties.get(JAVA_8).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(ASYNC)) {
|
||||
this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString()));
|
||||
}
|
||||
@@ -177,6 +186,15 @@ public class SpringCodegen extends AbstractJavaCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
if ("threetenbp".equals(dateLibrary)) {
|
||||
supportingFiles.add(new SupportingFile("customInstantDeserializer.mustache",
|
||||
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "CustomInstantDeserializer.java"));
|
||||
if (library.equals(DEFAULT_LIBRARY) || library.equals(SPRING_CLOUD_LIBRARY)) {
|
||||
supportingFiles.add(new SupportingFile("jacksonConfiguration.mustache",
|
||||
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "JacksonConfiguration.java"));
|
||||
}
|
||||
}
|
||||
|
||||
if (this.java8) {
|
||||
additionalProperties.put("javaVersion", "1.8");
|
||||
additionalProperties.put("jdk8", "true");
|
||||
@@ -230,7 +248,7 @@ public class SpringCodegen extends AbstractJavaCodegen {
|
||||
basePath = basePath.substring(0, pos);
|
||||
}
|
||||
|
||||
if (basePath == "") {
|
||||
if (basePath.equals("")) {
|
||||
basePath = "default";
|
||||
} else {
|
||||
co.subresourceOperation = !co.path.isEmpty();
|
||||
|
||||
Reference in New Issue
Block a user