Skip to content

Commit

Permalink
replace deprecated ISO8601Utils with StdDateFormat (#17052)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosMarc authored Jan 11, 2024
1 parent be19c35 commit 76560e3
Show file tree
Hide file tree
Showing 29 changed files with 320 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,35 @@

package org.openapitools.codegen.online;

import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import com.fasterxml.jackson.databind.util.StdDateFormat;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;


public class RFC3339DateFormat extends ISO8601DateFormat {
public class RFC3339DateFormat extends DateFormat {

private static final long serialVersionUID = 1L;
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);

// Same as ISO8601DateFormat but serializing milliseconds.
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
String value = ISO8601Utils.format(date, true);
String value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
toAppendTo.append(value);
return toAppendTo;
}

}
@Override
public Date parse(String source, ParsePosition pos) {
return sdf.parse(source, pos);
}

}
18 changes: 13 additions & 5 deletions modules/openapi-generator/src/main/resources/Java/JSON.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package {{invokerPackage}};

import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
Expand All @@ -33,11 +33,14 @@ import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;

public class JSON {
private Gson gson;
Expand All @@ -54,6 +57,11 @@ public class JSON {
{{/jsr310}}
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();

private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
Expand Down Expand Up @@ -461,7 +469,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
return new java.sql.Date(sdf.parse(date).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -471,7 +479,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
* If the dateFormat is null, DateTimeFormatter will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -496,7 +504,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
}
out.value(value);
}
Expand All @@ -515,7 +523,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
return sdf.parse(date);
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package {{invokerPackage}};

import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
Expand All @@ -27,14 +27,16 @@ import java.io.StringReader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;

/*
* A JSON utility class
Expand All @@ -57,6 +59,11 @@ public class JSON {
{{/jsr310}}
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();

private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
Expand Down Expand Up @@ -459,7 +466,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
return new java.sql.Date(sdf.parse(date).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -469,7 +476,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
* If the dateFormat is null, DateTimeFormatter will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -494,7 +501,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
}
out.value(value);
}
Expand All @@ -513,7 +520,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
return sdf.parse(date);
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@
<version>${gson-fire-version}</version>
</dependency>
{{/gson}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
{{#jackson}}
<!-- JSON processing: jackson -->
<dependency>
Expand All @@ -281,10 +286,6 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
Expand Down Expand Up @@ -351,9 +352,9 @@
{{#joda}}
<jodatime-version>2.10.5</jodatime-version>
{{/joda}}
<jackson-databind-version>2.15.2</jackson-databind-version>
{{#jackson}}
<jackson-version>2.15.2</jackson-version>
<jackson-databind-version>2.15.2</jackson-databind-version>
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
{{/jackson}}
{{#useJakartaEe}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package {{invokerPackage}};

import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
Expand All @@ -32,11 +32,14 @@ import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;

public class JSON {
private Gson gson;
Expand All @@ -51,6 +54,11 @@ public class JSON {
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
{{/jsr310}}

private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
{{#models}}{{#model}}{{#discriminator}} .registerTypeSelector({{classname}}.class, new TypeSelector() {
Expand Down Expand Up @@ -362,7 +370,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
return new java.sql.Date(sdf.parse(date).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -372,7 +380,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
* If the dateFormat is null, DateTimeFormatter will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -398,7 +406,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
}
out.value(value);
}
Expand All @@ -417,7 +425,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
return sdf.parse(date);
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand Down Expand Up @@ -296,11 +301,6 @@
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
{{#openApiNullable}}
<dependency>
<groupId>org.openapitools</groupId>
Expand Down Expand Up @@ -362,9 +362,9 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.9.0</gson-fire-version>
<swagger-annotations-version>1.6.3</swagger-annotations-version>
<jackson-databind-version>2.15.2</jackson-databind-version>
{{#usePlayWS}}
<jackson-version>2.15.2</jackson-version>
<jackson-databind-version>2.15.2</jackson-databind-version>
<play-version>2.6.7</play-version>
{{#openApiNullable}}
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
Expand Down
Loading

0 comments on commit 76560e3

Please sign in to comment.