Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] fix generation for JavaTimeFormatter #8348

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bin/configs/java-jersey2-java8-localdatetime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generatorName: java
outputDir: samples/client/petstore/java/jersey2-java8-localdatetime
library: jersey2
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
artifactId: petstore-jersey2-java8-localdatetime
hideGenerationTimestamp: "true"
dateLibrary: java8-localdatetime
java8: true
delegatePattern: true
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ public void processOpts() {
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))) {
// helper for client library that allow to parse/format java.time.OffsetDateTime or org.threeten.bp.OffsetDateTime
if (additionalProperties.containsKey("jsr310") && (isLibrary(WEBCLIENT) || isLibrary(VERTX) || isLibrary(RESTTEMPLATE) || isLibrary(RESTEASY) || isLibrary(MICROPROFILE) || isLibrary(JERSEY1) || isLibrary(JERSEY2))) {
supportingFiles.add(new SupportingFile("JavaTimeFormatter.mustache", invokerFolder, "JavaTimeFormatter.java"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import com.fasterxml.jackson.datatype.joda.JodaModule;
{{/joda}}
{{#java8}}
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
{{^threetenbp}}
import java.time.OffsetDateTime;
{{/threetenbp}}
{{/java8}}
{{#threetenbp}}
import com.fasterxml.jackson.datatype.threetenbp.ThreeTenModule;
Expand Down Expand Up @@ -68,7 +70,7 @@ import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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 @@ -500,9 +502,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}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
@@ -1,9 +1,16 @@
{{>licenseInfo}}
package {{invokerPackage}};

{{^threetenbp}}
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
{{/threetenbp}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeParseException;
{{/threetenbp}}

/**
* Class that add parsing/formatting support for Java 8+ {@code OffsetDateTime} class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
{{#java8}}
{{#jsr310}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
{{/threetenbp}}
{{^threetenbp}}
import java.time.OffsetDateTime;
{{/java8}}
{{/threetenbp}}
{{/jsr310}}

import java.net.URLEncoder;

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

{{>generatedAnnotation}}
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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 @@ -720,9 +725,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}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,9 +22,14 @@ import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
{{#java8}}
{{#jsr310}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
{{/threetenbp}}
{{^threetenbp}}
import java.time.OffsetDateTime;
{{/java8}}
{{/threetenbp}}
{{/jsr310}}

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

{{>generatedAnnotation}}
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
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 @@ -336,9 +341,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}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,9 +66,9 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
{{#java8}}
{{#jsr310}}{{^threetenbp}}
import java.time.OffsetDateTime;
{{/java8}}
{{/threetenbp}}{{/jsr310}}

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

{{>generatedAnnotation}}
@Component("{{invokerPackage}}.ApiClient")
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
public enum CollectionFormat {
CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);

Expand Down Expand Up @@ -407,9 +407,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate( (Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}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,9 +33,14 @@ import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

{{#java8}}
{{#jsr310}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
{{/threetenbp}}
{{^threetenbp}}
import java.time.OffsetDateTime;
{{/java8}}
{{/threetenbp}}
{{/jsr310}}
import java.text.DateFormat;
import java.util.*;
import java.util.function.Consumer;
Expand All @@ -45,7 +50,7 @@ import java.util.regex.Pattern;
import static java.util.stream.Collectors.toMap;

{{>generatedAnnotation}}
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

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 @@ -298,9 +303,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate((Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}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,9 +59,14 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;
{{#java8}}
{{#jsr310}}
{{#threetenbp}}
import org.threeten.bp.OffsetDateTime;
{{/threetenbp}}
{{^threetenbp}}
import java.time.OffsetDateTime;
{{/java8}}
{{/threetenbp}}
{{/jsr310}}

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

{{>generatedAnnotation}}
public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
public enum CollectionFormat {
CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);

Expand Down Expand Up @@ -357,9 +362,9 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
return "";
} else if (param instanceof Date) {
return formatDate( (Date) param);
} {{#java8}}else if (param instanceof OffsetDateTime) {
} {{#jsr310}}else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime((OffsetDateTime) param);
} {{/java8}}else if (param instanceof Collection) {
} {{/jsr310}}else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection<?>) param) {
if(b.length() > 0) {
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@
<module>samples/client/petstore/java/resttemplate-withXml</module>
<module>samples/client/petstore/java/webclient</module>
<module>samples/client/petstore/java/vertx</module>
<module>samples/client/petstore/java/jersey2-java8-localdatetime</module>
<module>samples/client/petstore/java/resteasy</module>
<module>samples/client/petstore/java/google-api-client</module>
<module>samples/client/petstore/java/rest-assured</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ 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/CustomInstantDeserializer.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 @@ -59,7 +59,7 @@
import org.openapitools.client.auth.OAuth;

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ApiClient {
public class ApiClient extends JavaTimeFormatter {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
private String basePath = "http://petstore.swagger.io:80/v2";
Expand Down Expand Up @@ -443,6 +443,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 org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.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
@@ -0,0 +1,21 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# exclude jar for gradle wrapper
!gradle/wrapper/*.jar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# build files
**/target
target
.gradle
build
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Loading