[JAVA: okhttp-gson, rest-assured, retrofit2] Don't generate Jackson import when serialization library is GSON (#18811)

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

This partially reverts commit 76560e34c9, 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 #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
This commit is contained in:
Philzen
2024-06-02 15:42:55 +02:00
committed by GitHub
parent 051abb82dc
commit ed2aad6756
44 changed files with 1340 additions and 2383 deletions

View File

@@ -102,8 +102,6 @@ test {
ext {
swagger_annotations_version = "1.6.11"
jackson_version = "2.17.1"
jackson_databind_version = "2.17.1"
jackson_databind_nullable_version = "0.2.6"
jakarta_annotation_version = "1.3.5"
feign_version = "10.12"
@@ -116,15 +114,10 @@ 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"
implementation "io.github.openfeign:feign-jackson:$feign_version"
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"
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 "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
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"

View File

@@ -12,15 +12,9 @@ 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",
"io.github.openfeign" % "feign-jackson" % "10.12" % "compile",
"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",
"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",
"com.github.scribejava" % "scribejava-core" % "8.0.0" % "compile",
"com.brsanthu" % "migbase64" % "2.2" % "compile",
"jakarta.annotation" % "jakarta.annotation-api" % "1.3.5" % "compile",

View File

@@ -304,7 +304,6 @@
<feign-form-version>3.8.0</feign-form-version>
<gson-version>2.10.1</gson-version>
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
<jackson-databind-version>2.17.1</jackson-databind-version>
<jakarta-annotation-version>1.3.5</jakarta-annotation-version>
<junit-version>5.10.0</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version>

View File

@@ -13,11 +13,11 @@
package org.openapitools.client;
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;
@@ -31,16 +31,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
@@ -57,11 +55,6 @@ public class JSON {
private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
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()
@@ -338,7 +331,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);
}
@@ -348,7 +341,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> {
@@ -373,7 +366,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);
}
@@ -392,7 +385,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);
}

View File

@@ -13,11 +13,11 @@
package org.openapitools.client;
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;
@@ -31,16 +31,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
@@ -57,11 +55,6 @@ public class JSON {
private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
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()
@@ -342,7 +335,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);
}
@@ -352,7 +345,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> {
@@ -377,7 +370,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);
}
@@ -396,7 +389,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);
}