Skip to content

Commit ed2aad6

Browse files
authored
[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 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 #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
1 parent 051abb8 commit ed2aad6

File tree

44 files changed

+1307
-2350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1307
-2350
lines changed

modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/RFC3339DateFormat.java

+7-19
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,23 @@
1717

1818
package org.openapitools.codegen.online;
1919

20-
import com.fasterxml.jackson.databind.util.StdDateFormat;
20+
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
21+
import com.fasterxml.jackson.databind.util.ISO8601Utils;
2122

22-
import java.text.DateFormat;
2323
import java.text.FieldPosition;
24-
import java.text.ParsePosition;
25-
import java.time.ZoneId;
26-
import java.time.ZoneOffset;
27-
import java.time.format.DateTimeFormatter;
2824
import java.util.Date;
29-
import java.util.TimeZone;
3025

31-
public class RFC3339DateFormat extends DateFormat {
26+
27+
public class RFC3339DateFormat extends ISO8601DateFormat {
3228

3329
private static final long serialVersionUID = 1L;
34-
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
35-
private static final StdDateFormat sdf = new StdDateFormat()
36-
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
37-
.withColonInTimeZone(true);
3830

31+
// Same as ISO8601DateFormat but serializing milliseconds.
3932
@Override
4033
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
41-
String value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
34+
String value = ISO8601Utils.format(date, true);
4235
toAppendTo.append(value);
4336
return toAppendTo;
4437
}
4538

46-
@Override
47-
public Date parse(String source, ParsePosition pos) {
48-
return sdf.parse(source, pos);
49-
}
50-
51-
}
39+
}

modules/openapi-generator/src/main/resources/Java/JSON.mustache

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
package {{invokerPackage}};
44

5-
import com.fasterxml.jackson.databind.util.StdDateFormat;
65
import com.google.gson.Gson;
76
import com.google.gson.GsonBuilder;
87
import com.google.gson.JsonParseException;
98
import com.google.gson.TypeAdapter;
9+
import com.google.gson.internal.bind.util.ISO8601Utils;
1010
import com.google.gson.stream.JsonReader;
1111
import com.google.gson.stream.JsonWriter;
1212
import com.google.gson.JsonElement;
@@ -33,14 +33,11 @@ import java.text.ParseException;
3333
import java.text.ParsePosition;
3434
import java.time.LocalDate;
3535
import java.time.OffsetDateTime;
36-
import java.time.ZoneOffset;
37-
import java.time.ZoneId;
3836
import java.time.format.DateTimeFormatter;
3937
import java.util.Date;
4038
import java.util.Locale;
4139
import java.util.Map;
4240
import java.util.HashMap;
43-
import java.util.TimeZone;
4441

4542
public class JSON {
4643
private Gson gson;
@@ -57,11 +54,6 @@ public class JSON {
5754
{{/jsr310}}
5855
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
5956

60-
private static final StdDateFormat sdf = new StdDateFormat()
61-
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
62-
.withColonInTimeZone(true);
63-
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
64-
6557
@SuppressWarnings("unchecked")
6658
public static GsonBuilder createGson() {
6759
GsonFireBuilder fireBuilder = new GsonFireBuilder()
@@ -469,7 +461,7 @@ public class JSON {
469461
if (dateFormat != null) {
470462
return new java.sql.Date(dateFormat.parse(date).getTime());
471463
}
472-
return new java.sql.Date(sdf.parse(date).getTime());
464+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
473465
} catch (ParseException e) {
474466
throw new JsonParseException(e);
475467
}
@@ -479,7 +471,7 @@ public class JSON {
479471

480472
/**
481473
* Gson TypeAdapter for java.util.Date type
482-
* If the dateFormat is null, DateTimeFormatter will be used.
474+
* If the dateFormat is null, ISO8601Utils will be used.
483475
*/
484476
public static class DateTypeAdapter extends TypeAdapter<Date> {
485477
@@ -504,7 +496,7 @@ public class JSON {
504496
if (dateFormat != null) {
505497
value = dateFormat.format(date);
506498
} else {
507-
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
499+
value = ISO8601Utils.format(date, true);
508500
}
509501
out.value(value);
510502
}
@@ -523,7 +515,7 @@ public class JSON {
523515
if (dateFormat != null) {
524516
return dateFormat.parse(date);
525517
}
526-
return sdf.parse(date);
518+
return ISO8601Utils.parse(date, new ParsePosition(0));
527519
} catch (ParseException e) {
528520
throw new JsonParseException(e);
529521
}

modules/openapi-generator/src/main/resources/Java/libraries/feign/build.gradle.mustache

+10-4
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ test {
102102

103103
ext {
104104
swagger_annotations_version = "1.6.11"
105+
{{#jackson}}
105106
jackson_version = "2.17.1"
106107
jackson_databind_version = "2.17.1"
108+
{{/jackson}}
107109
{{#openApiNullable}}
108110
jackson_databind_nullable_version = "0.2.6"
109111
{{/openApiNullable}}
@@ -118,20 +120,24 @@ dependencies {
118120
implementation "io.swagger:swagger-annotations:$swagger_annotations_version"
119121
implementation "com.google.code.findbugs:jsr305:3.0.2"
120122
implementation "io.github.openfeign:feign-core:$feign_version"
123+
{{#jackson}}
121124
implementation "io.github.openfeign:feign-jackson:$feign_version"
125+
{{/jackson}}
122126
implementation "io.github.openfeign:feign-slf4j:$feign_version"
123127
implementation "io.github.openfeign:feign-okhttp:$feign_version"
124128
implementation "io.github.openfeign.form:feign-form:$feign_form_version"
129+
{{#jackson}}
130+
{{#joda}}
131+
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson_version"
132+
{{/joda}}
125133
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
126134
implementation "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
127135
implementation "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
136+
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
137+
{{/jackson}}
128138
{{#openApiNullable}}
129139
implementation "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
130140
{{/openApiNullable}}
131-
{{#joda}}
132-
implementation "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jackson_version"
133-
{{/joda}}
134-
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
135141
implementation "com.brsanthu:migbase64:2.2"
136142
implementation "com.github.scribejava:scribejava-core:$scribejava_version"
137143
implementation "com.brsanthu:migbase64:2.2"

modules/openapi-generator/src/main/resources/Java/libraries/feign/build.sbt.mustache

+4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ lazy val root = (project in file(".")).
1212
"io.swagger" % "swagger-annotations" % "1.6.11" % "compile",
1313
"com.google.code.findbugs" % "jsr305" % "3.0.2" % "compile",
1414
"io.github.openfeign" % "feign-core" % "10.12" % "compile",
15+
{{#jackson}}
1516
"io.github.openfeign" % "feign-jackson" % "10.12" % "compile",
17+
{{/jackson}}
1618
"io.github.openfeign" % "feign-slf4j" % "10.12" % "compile",
1719
"io.github.openfeign.form" % "feign-form" % "3.8.0" % "compile",
1820
"io.github.openfeign" % "feign-okhttp" % "10.12" % "compile",
21+
{{#jackson}}
1922
"com.fasterxml.jackson.core" % "jackson-core" % "2.17.1" % "compile",
2023
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.17.1" % "compile",
2124
"com.fasterxml.jackson.core" % "jackson-databind" % "2.17.1" % "compile",
2225
"com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % "2.17.1" % "compile",
2326
"com.github.joschi.jackson" % "jackson-datatype-threetenbp" % "2.15.2" % "compile",
27+
{{/jackson}}
2428
"com.github.scribejava" % "scribejava-core" % "8.0.0" % "compile",
2529
"com.brsanthu" % "migbase64" % "2.2" % "compile",
2630
"jakarta.annotation" % "jakarta.annotation-api" % "1.3.5" % "compile",

modules/openapi-generator/src/main/resources/Java/libraries/feign/pom.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,14 @@
393393
<feign-form-version>3.8.0</feign-form-version>
394394
{{#jackson}}
395395
<jackson-version>2.17.1</jackson-version>
396+
<jackson-databind-version>2.17.1</jackson-databind-version>
396397
{{/jackson}}
397398
{{#gson}}
398399
<gson-version>2.10.1</gson-version>
399400
{{/gson}}
400401
{{#openApiNullable}}
401402
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
402403
{{/openApiNullable}}
403-
<jackson-databind-version>2.17.1</jackson-databind-version>
404404
{{#useJakartaEe}}
405405
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
406406
{{/useJakartaEe}}

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache

+6-13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
package {{invokerPackage}};
44

5-
import com.fasterxml.jackson.databind.util.StdDateFormat;
65
import com.google.gson.Gson;
76
import com.google.gson.GsonBuilder;
87
import com.google.gson.JsonParseException;
98
import com.google.gson.TypeAdapter;
9+
import com.google.gson.internal.bind.util.ISO8601Utils;
1010
import com.google.gson.stream.JsonReader;
1111
import com.google.gson.stream.JsonWriter;
1212
import com.google.gson.JsonElement;
@@ -27,16 +27,14 @@ import java.io.StringReader;
2727
import java.lang.reflect.Type;
2828
import java.text.DateFormat;
2929
import java.text.ParseException;
30+
import java.text.ParsePosition;
3031
import java.time.LocalDate;
3132
import java.time.OffsetDateTime;
32-
import java.time.ZoneId;
33-
import java.time.ZoneOffset;
3433
import java.time.format.DateTimeFormatter;
3534
import java.util.Date;
3635
import java.util.Locale;
3736
import java.util.Map;
3837
import java.util.HashMap;
39-
import java.util.TimeZone;
4038

4139
/*
4240
* A JSON utility class
@@ -59,11 +57,6 @@ public class JSON {
5957
{{/jsr310}}
6058
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
6159

62-
private static final StdDateFormat sdf = new StdDateFormat()
63-
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
64-
.withColonInTimeZone(true);
65-
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
66-
6760
@SuppressWarnings("unchecked")
6861
public static GsonBuilder createGson() {
6962
GsonFireBuilder fireBuilder = new GsonFireBuilder()
@@ -466,7 +459,7 @@ public class JSON {
466459
if (dateFormat != null) {
467460
return new java.sql.Date(dateFormat.parse(date).getTime());
468461
}
469-
return new java.sql.Date(sdf.parse(date).getTime());
462+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
470463
} catch (ParseException e) {
471464
throw new JsonParseException(e);
472465
}
@@ -476,7 +469,7 @@ public class JSON {
476469

477470
/**
478471
* Gson TypeAdapter for java.util.Date type
479-
* If the dateFormat is null, DateTimeFormatter will be used.
472+
* If the dateFormat is null, ISO8601Utils will be used.
480473
*/
481474
public static class DateTypeAdapter extends TypeAdapter<Date> {
482475
@@ -501,7 +494,7 @@ public class JSON {
501494
if (dateFormat != null) {
502495
value = dateFormat.format(date);
503496
} else {
504-
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
497+
value = ISO8601Utils.format(date, true);
505498
}
506499
out.value(value);
507500
}
@@ -520,7 +513,7 @@ public class JSON {
520513
if (dateFormat != null) {
521514
return dateFormat.parse(date);
522515
}
523-
return sdf.parse(date);
516+
return ISO8601Utils.parse(date, new ParsePosition(0));
524517
} catch (ParseException e) {
525518
throw new JsonParseException(e);
526519
}

modules/openapi-generator/src/main/resources/Java/libraries/rest-assured/pom.mustache

+5-6
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,6 @@
271271
<version>${gson-fire-version}</version>
272272
</dependency>
273273
{{/gson}}
274-
<dependency>
275-
<groupId>com.fasterxml.jackson.core</groupId>
276-
<artifactId>jackson-databind</artifactId>
277-
<version>${jackson-databind-version}</version>
278-
</dependency>
279274
{{#jackson}}
280275
<!-- JSON processing: jackson -->
281276
<dependency>
@@ -286,6 +281,10 @@
286281
<groupId>com.fasterxml.jackson.core</groupId>
287282
<artifactId>jackson-annotations</artifactId>
288283
</dependency>
284+
<dependency>
285+
<groupId>com.fasterxml.jackson.core</groupId>
286+
<artifactId>jackson-databind</artifactId>
287+
</dependency>
289288
<dependency>
290289
<groupId>org.openapitools</groupId>
291290
<artifactId>jackson-databind-nullable</artifactId>
@@ -352,9 +351,9 @@
352351
{{#joda}}
353352
<jodatime-version>2.10.5</jodatime-version>
354353
{{/joda}}
355-
<jackson-databind-version>2.17.1</jackson-databind-version>
356354
{{#jackson}}
357355
<jackson-version>2.17.1</jackson-version>
356+
<jackson-databind-version>2.17.1</jackson-databind-version>
358357
<jackson-databind-nullable-version>0.2.6</jackson-databind-nullable-version>
359358
{{/jackson}}
360359
{{#useJakartaEe}}

modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/JSON.mustache

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
package {{invokerPackage}};
44

5-
import com.fasterxml.jackson.databind.util.StdDateFormat;
65
import com.google.gson.Gson;
76
import com.google.gson.GsonBuilder;
87
import com.google.gson.JsonParseException;
98
import com.google.gson.TypeAdapter;
9+
import com.google.gson.internal.bind.util.ISO8601Utils;
1010
import com.google.gson.stream.JsonReader;
1111
import com.google.gson.stream.JsonWriter;
1212
import com.google.gson.JsonElement;
@@ -32,14 +32,11 @@ import java.text.ParseException;
3232
import java.text.ParsePosition;
3333
import java.time.LocalDate;
3434
import java.time.OffsetDateTime;
35-
import java.time.ZoneOffset;
36-
import java.time.ZoneId;
3735
import java.time.format.DateTimeFormatter;
3836
import java.util.Date;
3937
import java.util.Locale;
4038
import java.util.Map;
4139
import java.util.HashMap;
42-
import java.util.TimeZone;
4340

4441
public class JSON {
4542
private Gson gson;
@@ -54,11 +51,6 @@ public class JSON {
5451
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
5552
{{/jsr310}}
5653

57-
private static final StdDateFormat sdf = new StdDateFormat()
58-
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
59-
.withColonInTimeZone(true);
60-
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
61-
6254
public static GsonBuilder createGson() {
6355
GsonFireBuilder fireBuilder = new GsonFireBuilder()
6456
{{#models}}{{#model}}{{#discriminator}} .registerTypeSelector({{classname}}.class, new TypeSelector() {
@@ -370,7 +362,7 @@ public class JSON {
370362
if (dateFormat != null) {
371363
return new java.sql.Date(dateFormat.parse(date).getTime());
372364
}
373-
return new java.sql.Date(sdf.parse(date).getTime());
365+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
374366
} catch (ParseException e) {
375367
throw new JsonParseException(e);
376368
}
@@ -380,7 +372,7 @@ public class JSON {
380372

381373
/**
382374
* Gson TypeAdapter for java.util.Date type
383-
* If the dateFormat is null, DateTimeFormatter will be used.
375+
* If the dateFormat is null, ISO8601Utils will be used.
384376
*/
385377
public static class DateTypeAdapter extends TypeAdapter<Date> {
386378
@@ -406,7 +398,7 @@ public class JSON {
406398
if (dateFormat != null) {
407399
value = dateFormat.format(date);
408400
} else {
409-
value = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
401+
value = ISO8601Utils.format(date, true);
410402
}
411403
out.value(value);
412404
}
@@ -425,7 +417,7 @@ public class JSON {
425417
if (dateFormat != null) {
426418
return dateFormat.parse(date);
427419
}
428-
return sdf.parse(date);
420+
return ISO8601Utils.parse(date, new ParsePosition(0));
429421
} catch (ParseException e) {
430422
throw new JsonParseException(e);
431423
}

0 commit comments

Comments
 (0)