diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index 499469238d2f..f52c6e17238a 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -523,6 +523,17 @@ public class ApiClient { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link {{invokerPackage}}.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + {{/jsr310}} /** *

Set LenientOnJson.

diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache index eeb26cb9ca57..e285a2a3ef45 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache @@ -33,8 +33,10 @@ import java.text.ParseException; import java.text.ParsePosition; {{#jsr310}} import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; {{/jsr310}} import java.util.Date; import java.util.Locale; @@ -59,6 +61,7 @@ public class JSON { {{#jsr310}} private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); {{/jsr310}} private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @@ -125,6 +128,7 @@ public class JSON { {{#jsr310}} gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); {{/jsr310}} gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); {{#models}} @@ -431,6 +435,55 @@ public class JSON { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -439,6 +492,10 @@ public class JSON { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + {{/jsr310}} /** * Gson TypeAdapter for java.sql.Date type diff --git a/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/JSON.java b/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/JSON.java index 2ba91e749c71..2fa395e93749 100644 --- a/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -302,6 +306,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -310,6 +363,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java index 4ce1e392bbf3..6fd0329bbd3d 100644 --- a/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java @@ -381,6 +381,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java b/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java index 770195b00c99..a196e7a1737e 100644 --- a/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Bird.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); @@ -306,6 +310,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -314,6 +367,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java index fd046d2eeac0..927f5fb14ad8 100644 --- a/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java @@ -377,6 +377,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/JSON.java b/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/JSON.java index 578186471f75..6f53445342ba 100644 --- a/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.MyExampleGet200Response.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.OneOf1.CustomTypeAdapterFactory()); @@ -298,6 +302,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -306,6 +359,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java index be44254f833d..80421326fbb2 100644 --- a/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java @@ -377,6 +377,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/JSON.java b/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/JSON.java index 4587781f631b..636b0be4276b 100644 --- a/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.MyExamplePostRequest.CustomTypeAdapterFactory()); gson = gsonBuilder.create(); @@ -297,6 +301,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -305,6 +358,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java index 60b3515fb225..fb45dd214c99 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java @@ -377,6 +377,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/JSON.java b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/JSON.java index 70dfd4eb613f..b7493080a3c6 100644 --- a/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SimpleOneOf.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.SomeObj.CustomTypeAdapterFactory()); @@ -298,6 +302,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -306,6 +359,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/JSON.java index a07ae7226562..67895c54d943 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-3.1-duplicated-operationid/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gson = gsonBuilder.create(); } @@ -296,6 +300,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -304,6 +357,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java index 67cd90e25b1e..cbf384612db4 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -124,6 +127,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AllOfSimpleModel.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AnyTypeTest.CustomTypeAdapterFactory()); @@ -350,6 +354,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -358,6 +411,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java index 2c17437f7a6c..98357049decb 100644 --- a/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/ApiClient.java @@ -457,6 +457,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/JSON.java index 2ba91e749c71..2fa395e93749 100644 --- a/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-awsv4signature/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -302,6 +306,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -310,6 +363,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java index a7063d3e8b1a..3cdf0e35a798 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java @@ -470,6 +470,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/JSON.java index 19fee2967447..439fcc32a1b4 100644 --- a/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -135,6 +138,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesAnyType.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesArray.CustomTypeAdapterFactory()); @@ -377,6 +381,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -385,6 +438,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/JSON.java index 2ba91e749c71..2fa395e93749 100644 --- a/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -302,6 +306,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -310,6 +363,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/ApiClient.java index 7dc6b114022f..fa15f143339a 100644 --- a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/ApiClient.java @@ -456,6 +456,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/JSON.java index 23cee891c1a0..78c49cef71d2 100644 --- a/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-nullable-required/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -304,6 +308,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -312,6 +365,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java index 1b92abefe987..ef0d2651c8d0 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java @@ -459,6 +459,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/JSON.java index 19fee2967447..439fcc32a1b4 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -135,6 +138,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesAnyType.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesArray.CustomTypeAdapterFactory()); @@ -377,6 +381,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -385,6 +438,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/JSON.java index 2ba91e749c71..2fa395e93749 100644 --- a/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-swagger1/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -302,6 +306,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -310,6 +363,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/ApiClient.java index 9dc6db9bebfd..5725bd1f29b8 100644 --- a/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/JSON.java index 2ba91e749c71..2fa395e93749 100644 --- a/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-swagger2/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -95,6 +98,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Category.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelApiResponse.CustomTypeAdapterFactory()); @@ -302,6 +306,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -310,6 +363,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java index b196c0b9313f..8972870e891d 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java @@ -505,6 +505,17 @@ public ApiClient setLocalDateFormat(DateTimeFormatter dateFormat) { return this; } + /** + *

Set LocalDateTimeFormat.

+ * + * @param dateFormat a {@link java.time.format.DateTimeFormatter} object + * @return a {@link org.openapitools.client.ApiClient} object + */ + public ApiClient setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + JSON.setLocalDateTimeFormat(dateFormat); + return this; + } + /** *

Set LenientOnJson.

* diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java index 7062e2f15e41..01e0ba1f8eec 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java @@ -36,8 +36,10 @@ import java.text.ParseException; import java.text.ParsePosition; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -56,6 +58,7 @@ public class JSON { private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private static LocalDateTimeTypeAdapter localDateTimeTypeAdapter = new LocalDateTimeTypeAdapter(); private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); @SuppressWarnings("unchecked") @@ -243,6 +246,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter); gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter); + gsonBuilder.registerTypeAdapter(LocalDateTime.class, localDateTimeTypeAdapter); gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesClass.CustomTypeAdapterFactory()); gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AllOfModelArrayAnyOf.CustomTypeAdapterFactory()); @@ -549,6 +553,55 @@ public LocalDate read(JsonReader in) throws IOException { } } + /** + * Gson TypeAdapter for JSR310 LocalDateTime type + */ + public static class LocalDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + + public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + return LocalDateTime.parse(date, formatter); + } catch (DateTimeParseException e) { + if (date.length() > 10 && date.charAt(10) == ' ') { + date = date.substring(0, 10) + 'T' + date.substring(11); + return LocalDateTime.parse(date, formatter); + } + throw e; + } + } + } + } + public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { offsetDateTimeTypeAdapter.setFormat(dateFormat); } @@ -557,6 +610,10 @@ public static void setLocalDateFormat(DateTimeFormatter dateFormat) { localDateTypeAdapter.setFormat(dateFormat); } + public static void setLocalDateTimeFormat(DateTimeFormatter dateFormat) { + localDateTimeTypeAdapter.setFormat(dateFormat); + } + /** * Gson TypeAdapter for java.sql.Date type * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used diff --git a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java index 5b6b2241ce28..56019aaf4950 100644 --- a/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java +++ b/samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java @@ -16,6 +16,7 @@ import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; @@ -195,6 +196,39 @@ public void testLocalDateTypeAdapter() { assertEquals(json.deserialize(str, LocalDate.class), date); } + @Test + public void testLocalDateTimeTypeAdapter() { + final String str = "\"2016-09-09T08:02:03\""; + final LocalDateTime date = LocalDateTime.of(2016, 9, 9, 8, 2, 3); + + assertEquals(str, json.serialize(date)); + assertEquals(json.deserialize(str, LocalDateTime.class), date); + assertNull(json.deserialize("null", LocalDateTime.class)); + } + + @Test + public void testLocalDateTimeTypeAdapterWithSpaceSeparator() { + // RFC 3339 section 5.6 permits a space in place of the ISO 8601 'T' separator + final LocalDateTime date = LocalDateTime.of(2016, 9, 9, 8, 2, 3); + + assertEquals(json.deserialize("\"2016-09-09 08:02:03\"", LocalDateTime.class), date); + assertEquals("\"2016-09-09T08:02:03\"", json.serialize(date)); + } + + @Test + public void testLocalDateTimeTypeAdapterWithCustomFormat() { + final String str = "\"2016-09-09 08:02:03\""; + final LocalDateTime date = LocalDateTime.of(2016, 9, 9, 8, 2, 3); + + JSON.setLocalDateTimeFormat(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + try { + assertEquals(str, json.serialize(date)); + assertEquals(json.deserialize(str, LocalDateTime.class), date); + } finally { + JSON.setLocalDateTimeFormat(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } + } + @Test public void testDefaultDate() throws Exception { final DateTimeFormatter datetimeFormat = DateTimeFormatter.ISO_OFFSET_DATE_TIME; diff --git a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml index b5c5141bdd3d..859141904ec9 100644 --- a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml +++ b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/pom.xml @@ -269,7 +269,7 @@ UTF-8 - 7.0.8 + 7.0.5 3.1.5 3.0.0