Skip to content

Commit

Permalink
[JAVA: okhttp-gson, rest-assured, retrofit2] Don't generate Jackson i…
Browse files Browse the repository at this point in the history
…mport when serialization library is GSON (OpenAPITools#18811)

* Partially revert "replace deprecated ISO8601Utils with StdDateFormat (OpenAPITools#17052)"

This partially reverts commit 76560e3, namely anything
related to generators and samples using GSON instead of Jackson.

Changes to Jackson-only generation and generator-online regarding RFC3339DateFormat
are not being reverted.

* Test for default serialization library fallback

* Convert repetitive tests to parameterized test

* Add regression test for OpenAPITools#18515

* [FEIGN] Only include <jackson-databind-version> property in pom.xml when required

* [RETROFIT2] Only include jackson-databind in gradle file when actually required

* [FEIGN] Don't include jackson dep's in sbt file when GSON is selected

* [FEIGN] Don't include jackson dep's in gradle file when GSON is selected

* DRY refactor JavaClientCodegen test code, increase readability

- use fluent assertions
- use helper method newTempFolder()
- use Java 9 static factory methods for maps
- don't declare variables that are only used once
- group declarations and usages
- use non-blocking java.nio.file API wherever possible

* Regenerate samples
  • Loading branch information
Philzen authored and welshm committed Jun 5, 2024
1 parent 226a47f commit 9159562
Show file tree
Hide file tree
Showing 44 changed files with 1,307 additions and 2,350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,23 @@

package org.openapitools.codegen.online;

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

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 DateFormat {

public class RFC3339DateFormat extends ISO8601DateFormat {

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 = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
String value = ISO8601Utils.format(date, true);
toAppendTo.append(value);
return toAppendTo;
}

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

}
}
18 changes: 5 additions & 13 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,14 +33,11 @@ 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 @@ -57,11 +54,6 @@ 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 @@ -469,7 +461,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(sdf.parse(date).getTime());
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -479,7 +471,7 @@ public class JSON {

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

ext {
swagger_annotations_version = "1.6.11"
{{#jackson}}
jackson_version = "2.17.1"
jackson_databind_version = "2.17.1"
{{/jackson}}
{{#openApiNullable}}
jackson_databind_nullable_version = "0.2.6"
{{/openApiNullable}}
Expand All @@ -118,20 +120,24 @@ dependencies {
implementation "io.swagger:swagger-annotations:$swagger_annotations_version"
implementation "com.google.code.findbugs:jsr305:3.0.2"
implementation "io.github.openfeign:feign-core:$feign_version"
{{#jackson}}
implementation "io.github.openfeign:feign-jackson:$feign_version"
{{/jackson}}
implementation "io.github.openfeign:feign-slf4j:$feign_version"
implementation "io.github.openfeign:feign-okhttp:$feign_version"
implementation "io.github.openfeign.form:feign-form:$feign_form_version"
{{#jackson}}
{{#joda}}
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson_version"
{{/joda}}
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
{{/jackson}}
{{#openApiNullable}}
implementation "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
{{/openApiNullable}}
{{#joda}}
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson_version"
{{/joda}}
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
implementation "com.brsanthu:migbase64:2.2"
implementation "com.github.scribejava:scribejava-core:$scribejava_version"
implementation "com.brsanthu:migbase64:2.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ lazy val root = (project in file(".")).
"io.swagger" % "swagger-annotations" % "1.6.11" % "compile",
"com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile",
"io.github.openfeign" % "feign-core" % "10.12" % "compile",
{{#jackson}}
"io.github.openfeign" % "feign-jackson" % "10.12" % "compile",
{{/jackson}}
"io.github.openfeign" % "feign-slf4j" % "10.12" % "compile",
"io.github.openfeign.form" % "feign-form" % "3.8.0" % "compile",
"io.github.openfeign" % "feign-okhttp" % "10.12" % "compile",
{{#jackson}}
"com.fasterxml.jackson.core" % "jackson-core" % "2.17.1" % "compile",
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.17.1" % "compile",
"com.fasterxml.jackson.core" % "jackson-databind" % "2.17.1" % "compile",
"com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.17.1" % "compile",
"com.github.joschi.jackson" % "jackson-datatype-threetenbp" % "2.15.2" % "compile",
{{/jackson}}
"com.github.scribejava" % "scribejava-core" % "8.0.0" % "compile",
"com.brsanthu" % "migbase64" % "2.2" % "compile",
"jakarta.annotation" % "jakarta.annotation-api" % "1.3.5" % "compile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,14 @@
<feign-form-version>3.8.0</feign-form-version>
{{#jackson}}
<jackson-version>2.17.1</jackson-version>
<jackson-databind-version>2.17.1</jackson-databind-version>
{{/jackson}}
{{#gson}}
<gson-version>2.10.1</gson-version>
{{/gson}}
{{#openApiNullable}}
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
{{/openApiNullable}}
<jackson-databind-version>2.17.1</jackson-databind-version>
{{#useJakartaEe}}
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
{{/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 @@ -27,16 +27,14 @@ 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 @@ -59,11 +57,6 @@ 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 @@ -466,7 +459,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(sdf.parse(date).getTime());
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -476,7 +469,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, DateTimeFormatter will be used.
* If the dateFormat is null, ISO8601Utils will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -501,7 +494,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
value = ISO8601Utils.format(date, true);
}
out.value(value);
}
Expand All @@ -520,7 +513,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return sdf.parse(date);
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,6 @@
<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 @@ -286,6 +281,10 @@
<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 @@ -352,9 +351,9 @@
{{#joda}}
<jodatime-version>2.10.5</jodatime-version>
{{/joda}}
<jackson-databind-version>2.17.1</jackson-databind-version>
{{#jackson}}
<jackson-version>2.17.1</jackson-version>
<jackson-databind-version>2.17.1</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,14 +32,11 @@ 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,11 +51,6 @@ 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 @@ -370,7 +362,7 @@ public class JSON {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(sdf.parse(date).getTime());
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand All @@ -380,7 +372,7 @@ public class JSON {

/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, DateTimeFormatter will be used.
* If the dateFormat is null, ISO8601Utils will be used.
*/
public static class DateTypeAdapter extends TypeAdapter<Date> {
Expand All @@ -406,7 +398,7 @@ public class JSON {
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
value = ISO8601Utils.format(date, true);
}
out.value(value);
}
Expand All @@ -425,7 +417,7 @@ public class JSON {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return sdf.parse(date);
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
}
Expand Down
Loading

0 comments on commit 9159562

Please sign in to comment.