Skip to content

Commit

Permalink
[Java][Client] Use java8 OffsetDateTime for clients (#7190)
Browse files Browse the repository at this point in the history
* use java8 OffsetDateTime for clients

* use java8 OffsetDateTime for clients

* fix javadoc

* add javadoc to JavaTimeFormatter.mustache

* add javadoc to JavaTimeFormatter.mustache

* add javadoc to JavaTimeFormatter.mustache
  • Loading branch information
borsch authored Aug 31, 2020
1 parent b9662dc commit 14a500c
Show file tree
Hide file tree
Showing 24 changed files with 452 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4973,6 +4973,15 @@ public String getLibrary() {
return library;
}

/**
* check if current active library equals to passed
* @param library - library to be compared with
* @return {@code true} if passed library is active, {@code false} otherwise
*/
public final boolean isLibrary(String library) {
return library.equals(this.library);
}

/**
* Set Git host.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,16 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("ServerVariable.mustache", invokerFolder, "ServerVariable.java"));
supportingFiles.add(new SupportingFile("openapi.mustache", "api", "openapi.yaml"));

if (dateLibrary.equals("java8") && (isLibrary(WEBCLIENT) || isLibrary(VERTX) || isLibrary(RESTTEMPLATE) || isLibrary(RESTEASY) || isLibrary(MICROPROFILE) || isLibrary(JERSEY2))) {
supportingFiles.add(new SupportingFile("JavaTimeFormatter.mustache", invokerFolder, "JavaTimeFormatter.java"));
}

if (!(RESTTEMPLATE.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) {
if (!(RESTTEMPLATE.equals(getLibrary()) || isLibrary(REST_ASSURED) || isLibrary(NATIVE) || isLibrary(MICROPROFILE))) {
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
}

// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) {
if (!(isLibrary(GOOGLE_API_CLIENT) || isLibrary(REST_ASSURED) || isLibrary(NATIVE) || isLibrary(MICROPROFILE))) {
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
supportingFiles.add(new SupportingFile("auth/HttpBearerAuth.mustache", authFolder, "HttpBearerAuth.java"));
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.fasterxml.jackson.datatype.joda.JodaModule;
{{/joda}}
{{#java8}}
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.OffsetDateTime;
{{/java8}}
{{#threetenbp}}
import com.fasterxml.jackson.datatype.threetenbp.ThreeTenModule;
Expand Down Expand Up @@ -67,7 +68,7 @@ import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
private String basePath = "{{{basePath}}}";
Expand Down Expand Up @@ -499,7 +500,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection<?>)param) {
if(b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{>licenseInfo}}
package {{invokerPackage}};

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Class that add parsing/formatting support for Java 8+ {@code OffsetDateTime} class.
* It's generated for java clients when {@code AbstractJavaCodegen#dateLibrary} specified as {@code java8}.
*/
{{>generatedAnnotation}}
public class JavaTimeFormatter {
private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
/**
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
* @return DateTimeFormatter
*/
public DateTimeFormatter getOffsetDateTimeFormatter() {
return offsetDateTimeFormatter;
}

/**
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
*/
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
}

/**
* Parse the given string into {@code OffsetDateTime} object.
* @param str String
* @return {@code OffsetDateTime}
*/
public OffsetDateTime parseOffsetDateTime(String str) {
try {
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
} catch (DateTimeParseException e) {
throw new RuntimeException(e);
}
}
/**
* Format the given {@code OffsetDateTime} object into string.
* @param offsetDateTime {@code OffsetDateTime}
* @return {@code OffsetDateTime} in string format
*/
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
return offsetDateTimeFormatter.format(offsetDateTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
{{#java8}}
import java.time.OffsetDateTime;
{{/java8}}

import java.net.URLEncoder;

Expand All @@ -77,7 +80,7 @@ import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String basePath = "{{{basePath}}}";
Expand Down Expand Up @@ -695,7 +698,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection)param) {
if(b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
{{#java8}}
import java.time.OffsetDateTime;
{{/java8}}

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
Expand All @@ -48,7 +51,7 @@ import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
private String basePath = "{{{basePath}}}";
Expand Down Expand Up @@ -333,7 +336,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection)param) {
if(b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
{{#java8}}
import java.time.OffsetDateTime;
{{/java8}}

import {{invokerPackage}}.auth.Authentication;
{{#hasHttpBasicMethods}}
Expand All @@ -83,7 +86,7 @@ import {{invokerPackage}}.auth.OAuth;

{{>generatedAnnotation}}
@Component("{{invokerPackage}}.ApiClient")
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public enum CollectionFormat {
CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);
Expand Down Expand Up @@ -404,7 +407,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate( (Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection<?>) param) {
if(b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

{{#java8}}
import java.time.OffsetDateTime;
{{/java8}}
import java.text.DateFormat;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -42,7 +45,7 @@ import java.util.regex.Pattern;
import static java.util.stream.Collectors.toMap;

{{>generatedAnnotation}}
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
Expand Down Expand Up @@ -295,7 +298,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for (Object o : (Collection) param) {
if (b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
{{#java8}}
import java.time.OffsetDateTime;
{{/java8}}

import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
Expand All @@ -69,7 +72,7 @@ import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public enum CollectionFormat {
CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);
Expand Down Expand Up @@ -354,7 +357,9 @@ public class ApiClient {
return "";
} else if (param instanceof Date) {
return formatDate( (Date) param);
} else if (param instanceof Collection) {
} {{#java8}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection<?>) param) {
if(b.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ src/main/java/org/openapitools/client/ApiException.java
src/main/java/org/openapitools/client/ApiResponse.java
src/main/java/org/openapitools/client/Configuration.java
src/main/java/org/openapitools/client/JSON.java
src/main/java/org/openapitools/client/JavaTimeFormatter.java
src/main/java/org/openapitools/client/Pair.java
src/main/java/org/openapitools/client/RFC3339DateFormat.java
src/main/java/org/openapitools/client/ServerConfiguration.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.time.OffsetDateTime;

import java.net.URLEncoder;

Expand All @@ -64,7 +65,7 @@
import org.openapitools.client.auth.OAuth;

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ApiClient {
public class ApiClient extends JavaTimeFormatter {
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String basePath = "http://petstore.swagger.io:80/v2";
Expand Down Expand Up @@ -612,6 +613,8 @@ public String parameterToString(Object param) {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection)param) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package org.openapitools.client;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Class that add parsing/formatting support for Java 8+ {@code OffsetDateTime} class.
* It's generated for java clients when {@code AbstractJavaCodegen#dateLibrary} specified as {@code java8}.
*/
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class JavaTimeFormatter {

private DateTimeFormatter offsetDateTimeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

/**
* Get the date format used to parse/format {@code OffsetDateTime} parameters.
* @return DateTimeFormatter
*/
public DateTimeFormatter getOffsetDateTimeFormatter() {
return offsetDateTimeFormatter;
}

/**
* Set the date format used to parse/format {@code OffsetDateTime} parameters.
* @param offsetDateTimeFormatter {@code DateTimeFormatter}
*/
public void setOffsetDateTimeFormatter(DateTimeFormatter offsetDateTimeFormatter) {
this.offsetDateTimeFormatter = offsetDateTimeFormatter;
}

/**
* Parse the given string into {@code OffsetDateTime} object.
* @param str String
* @return {@code OffsetDateTime}
*/
public OffsetDateTime parseOffsetDateTime(String str) {
try {
return OffsetDateTime.parse(str, offsetDateTimeFormatter);
} catch (DateTimeParseException e) {
throw new RuntimeException(e);
}
}
/**
* Format the given {@code OffsetDateTime} object into string.
* @param offsetDateTime {@code OffsetDateTime}
* @return {@code OffsetDateTime} in string format
*/
public String formatOffsetDateTime(OffsetDateTime offsetDateTime) {
return offsetDateTimeFormatter.format(offsetDateTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ src/main/AndroidManifest.xml
src/main/java/org/openapitools/client/ApiClient.java
src/main/java/org/openapitools/client/ApiException.java
src/main/java/org/openapitools/client/Configuration.java
src/main/java/org/openapitools/client/JavaTimeFormatter.java
src/main/java/org/openapitools/client/Pair.java
src/main/java/org/openapitools/client/RFC3339DateFormat.java
src/main/java/org/openapitools/client/ServerConfiguration.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

import java.time.OffsetDateTime;
import java.text.DateFormat;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -38,7 +39,7 @@
import static java.util.stream.Collectors.toMap;

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ApiClient {
public class ApiClient extends JavaTimeFormatter {

private static final Pattern CONTENT_DISPOSITION_PATTERN = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
private static final OpenOptions FILE_DOWNLOAD_OPTIONS = new OpenOptions().setCreate(true).setTruncateExisting(true);
Expand Down Expand Up @@ -287,6 +288,8 @@ public String parameterToString(Object param) {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for (Object o : (Collection) param) {
Expand Down
Loading

0 comments on commit 14a500c

Please sign in to comment.