forked from loafle/openapi-generator-original
Merge pull request #3015 from cbornet/java_instant
Use OffsetDateTime instead of LocalDateTime
This commit is contained in:
commit
77d1d97da5
@ -127,6 +127,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
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-localdatetime", "Java 8 using LocalDateTime (for legacy app only)");
|
||||
dateOptions.put("joda", "Joda");
|
||||
dateOptions.put("legacy", "Legacy java.util.Date");
|
||||
dateLibrary.setEnum(dateOptions);
|
||||
@ -363,7 +364,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
if(additionalProperties.containsKey(DATE_LIBRARY)) {
|
||||
this.dateLibrary = additionalProperties.get(DATE_LIBRARY).toString();
|
||||
setDateLibrary(additionalProperties.get(DATE_LIBRARY).toString());
|
||||
additionalProperties.put(dateLibrary, "true");
|
||||
}
|
||||
|
||||
if("joda".equals(dateLibrary)) {
|
||||
@ -373,13 +375,17 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
importMapping.put("LocalDate", "org.joda.time.LocalDate");
|
||||
importMapping.put("DateTime", "org.joda.time.DateTime");
|
||||
}
|
||||
else if ("java8".equals(dateLibrary)) {
|
||||
else if (dateLibrary.startsWith("java8")) {
|
||||
additionalProperties.put("java8", "true");
|
||||
additionalProperties.put("javaVersion", "1.8");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("DateTime", "LocalDateTime");
|
||||
importMapping.put("LocalDate", "java.time.LocalDate");
|
||||
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
|
||||
if ("java8-localdatetime".equals(dateLibrary)) {
|
||||
typeMapping.put("DateTime", "LocalDateTime");
|
||||
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
|
||||
} else {
|
||||
typeMapping.put("DateTime", "OffsetDateTime");
|
||||
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
|
||||
}
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
@ -1032,7 +1038,5 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
this.useRxJava = useRxJava;
|
||||
}
|
||||
|
||||
public void setDateLibrary(String library) {
|
||||
this.dateLibrary = library;
|
||||
}
|
||||
public void setDateLibrary(String library) { this.dateLibrary = library; }
|
||||
}
|
||||
|
@ -89,16 +89,11 @@ public class JavaJerseyServerCodegen extends AbstractJavaJAXRSServerCodegen {
|
||||
implFolder = (String) additionalProperties.get(CodegenConstants.IMPL_FOLDER);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("dateLibrary")) {
|
||||
setDateLibrary(additionalProperties.get("dateLibrary").toString());
|
||||
additionalProperties.put(dateLibrary, "true");
|
||||
}
|
||||
|
||||
if ("joda".equals(dateLibrary)) {
|
||||
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
|
||||
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
|
||||
} else if ( "java8".equals(dateLibrary) ) {
|
||||
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
|
||||
} else if ( dateLibrary.startsWith("java8") ) {
|
||||
supportingFiles.add(new SupportingFile("OffsetDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "OffsetDateTimeProvider.java"));
|
||||
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
|
||||
}
|
||||
|
||||
|
@ -47,14 +47,6 @@ public class JavaResteasyServerCodegen extends JavaClientCodegen 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("joda", "Joda");
|
||||
dateLibrary.setEnum(dateOptions);
|
||||
|
||||
cliOptions.add(dateLibrary);
|
||||
|
||||
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
|
||||
library.setDefault(DEFAULT_LIBRARY);
|
||||
|
||||
@ -117,35 +109,16 @@ public class JavaResteasyServerCodegen extends JavaClientCodegen implements Code
|
||||
supportingFiles.add(new SupportingFile("StringUtil.mustache",
|
||||
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "StringUtil.java"));
|
||||
|
||||
if (additionalProperties.containsKey("dateLibrary")) {
|
||||
setDateLibrary(additionalProperties.get("dateLibrary").toString());
|
||||
additionalProperties.put(dateLibrary, "true");
|
||||
}
|
||||
|
||||
if ("joda".equals(dateLibrary)) {
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("DateTime", "DateTime");
|
||||
|
||||
importMapping.put("LocalDate", "org.joda.time.LocalDate");
|
||||
importMapping.put("DateTime", "org.joda.time.DateTime");
|
||||
|
||||
supportingFiles.add(new SupportingFile("JacksonConfig.mustache",
|
||||
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "JacksonConfig.java"));
|
||||
|
||||
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache",
|
||||
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
|
||||
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache",
|
||||
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
|
||||
} else if ("java8".equals(dateLibrary)) {
|
||||
additionalProperties.put("java8", "true");
|
||||
additionalProperties.put("javaVersion", "1.8");
|
||||
typeMapping.put("date", "LocalDate");
|
||||
typeMapping.put("DateTime", "LocalDateTime");
|
||||
importMapping.put("LocalDate", "java.time.LocalDate");
|
||||
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
|
||||
|
||||
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache",
|
||||
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
|
||||
} else if (dateLibrary.startsWith("java8")) {
|
||||
supportingFiles.add(new SupportingFile("OffsetDateTimeProvider.mustache",
|
||||
(sourceFolder + '/' + apiPackage).replace(".", "/"), "OffsetDateTimeProvider.java"));
|
||||
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache",
|
||||
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
|
||||
}
|
||||
|
@ -11,23 +11,23 @@ import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Provider
|
||||
public class LocalDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDateTime> {
|
||||
public class OffsetDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, OffsetDateTime> {
|
||||
private final UriInfo uriInfo;
|
||||
|
||||
public LocalDateTimeProvider(@Context UriInfo uriInfo) {
|
||||
super(LocalDateTime.class);
|
||||
public OffsetDateTimeProvider(@Context UriInfo uriInfo) {
|
||||
super(OffsetDateTime.class);
|
||||
this.uriInfo = uriInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Injectable<LocalDateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
|
||||
return new Injectable<LocalDateTime>() {
|
||||
public Injectable<OffsetDateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
|
||||
return new Injectable<OffsetDateTime>() {
|
||||
@Override
|
||||
public LocalDateTime getValue() {
|
||||
public OffsetDateTime getValue() {
|
||||
final List<String> values = uriInfo.getQueryParameters().get(a.value());
|
||||
|
||||
if (values == null || values.isEmpty())
|
||||
@ -37,7 +37,7 @@ public class LocalDateTimeProvider extends PerRequestTypeInjectableProvider<Quer
|
||||
entity(a.value() + " cannot contain multiple values").build());
|
||||
}
|
||||
|
||||
return LocalDateTime.parse(values.get(0));
|
||||
return OffsetDateTime.parse(values.get(0));
|
||||
}
|
||||
};
|
||||
}
|
@ -11,23 +11,23 @@ import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Provider
|
||||
public class LocalDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDateTime> {
|
||||
public class OffsetDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, OffsetDateTime> {
|
||||
private final UriInfo uriInfo;
|
||||
|
||||
public LocalDateTimeProvider(@Context UriInfo uriInfo) {
|
||||
super(LocalDateTime.class);
|
||||
public OffsetDateTimeProvider(@Context UriInfo uriInfo) {
|
||||
super(OffsetDateTime.class);
|
||||
this.uriInfo = uriInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Injectable<LocalDateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
|
||||
return new Injectable<LocalDateTime>() {
|
||||
public Injectable<OffsetDateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
|
||||
return new Injectable<OffsetDateTime>() {
|
||||
@Override
|
||||
public LocalDateTime getValue() {
|
||||
public OffsetDateTime getValue() {
|
||||
final List<String> values = uriInfo.getQueryParameters().get(a.value());
|
||||
|
||||
if (values == null || values.isEmpty())
|
||||
@ -37,7 +37,7 @@ public class LocalDateTimeProvider extends PerRequestTypeInjectableProvider<Quer
|
||||
entity(a.value() + " cannot contain multiple values").build());
|
||||
}
|
||||
|
||||
return LocalDateTime.parse(values.get(0));
|
||||
return OffsetDateTime.parse(values.get(0));
|
||||
}
|
||||
};
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import javax.ws.rs.ext.ParamConverter;
|
||||
import javax.ws.rs.ext.ParamConverterProvider;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@Provider
|
||||
public class LocalDateTimeProvider implements ParamConverterProvider {
|
||||
|
||||
public static class LocalDateTimeConverter implements ParamConverter<LocalDateTime> {
|
||||
|
||||
@Override
|
||||
public LocalDateTime fromString(String string) {
|
||||
LocalDateTime localDateTime = LocalDateTime.parse(string);
|
||||
return localDateTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(LocalDateTime t) {
|
||||
return t.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
|
||||
if (LocalDateTime.class.equals(type)) {
|
||||
return (ParamConverter<T>) new LocalDateTimeConverter();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import javax.ws.rs.ext.ParamConverter;
|
||||
import javax.ws.rs.ext.ParamConverterProvider;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@Provider
|
||||
public class OffsetDateTimeProvider implements ParamConverterProvider {
|
||||
|
||||
public static class OffsetDateTimeConverter implements ParamConverter<OffsetDateTime> {
|
||||
|
||||
@Override
|
||||
public OffsetDateTime fromString(String string) {
|
||||
OffsetDateTime OffsetDateTime = OffsetDateTime.parse(string);
|
||||
return OffsetDateTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(OffsetDateTime t) {
|
||||
return t.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
|
||||
if (OffsetDateTime.class.equals(type)) {
|
||||
return (ParamConverter<T>) new OffsetDateTimeConverter();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -34,6 +34,6 @@ public class JaxrsJava8ModelTest {
|
||||
Json.prettyPrint(cm);
|
||||
assertEquals(cm.vars.get(0).datatype, "Long");
|
||||
assertEquals(cm.vars.get(1).datatype, "LocalDate");
|
||||
assertEquals(cm.vars.get(2).datatype, "LocalDateTime");
|
||||
assertEquals(cm.vars.get(2).datatype, "OffsetDateTime");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user