Skip to content

Commit 8abbe79

Browse files
authored
[Java] fix Jackson 3 createDefaultMapper(null) losing the default date format (restclient, webclient) (#24625)
1 parent ae4b44c commit 8abbe79

7 files changed

Lines changed: 49 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
206206

207207
{{#useJackson3}}
208208
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
209+
if (null == dateFormat) {
210+
dateFormat = createDefaultDateFormat();
211+
}
209212
return JsonMapper.builder()
210213
.defaultDateFormat(dateFormat)
211214
{{#failOnUnknownProperties}}

modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
183183

184184
{{#useJackson3}}
185185
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
186+
if (null == dateFormat) {
187+
dateFormat = createDefaultDateFormat();
188+
}
186189
return JsonMapper.builder()
187190
.defaultDateFormat(dateFormat)
188191
{{#failOnUnknownProperties}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,6 +3671,37 @@ public void testRestClientWithXMLAndJackson3AndOpenApiNullable_issue_23860() {
36713671
);
36723672
}
36733673

3674+
@Test(description = "Regression test for issue #24588: with useJackson3=true the generated"
3675+
+ " createDefaultMapper must fall back to createDefaultDateFormat() when called with"
3676+
+ " null, like the Jackson 2 branch does. Otherwise date-time fields serialize as"
3677+
+ " epoch numbers instead of RFC 3339.")
3678+
public void testJackson3DefaultMapperFallsBackToDefaultDateFormat_issue_24588() {
3679+
for (String library : new String[]{JavaClientCodegen.RESTCLIENT, JavaClientCodegen.WEBCLIENT}) {
3680+
final Path output = newTempFolder();
3681+
final CodegenConfigurator configurator = new CodegenConfigurator()
3682+
.setGeneratorName(JAVA_GENERATOR)
3683+
.setLibrary(library)
3684+
.setAdditionalProperties(Map.of(
3685+
CodegenConstants.API_PACKAGE, "xyz.abcdef.api",
3686+
JavaClientCodegen.USE_JACKSON_3, true,
3687+
JavaClientCodegen.USE_SPRING_BOOT4, true,
3688+
JavaClientCodegen.OPENAPI_NULLABLE, false
3689+
))
3690+
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
3691+
.setOutputDir(output.toString().replace("\\", "/"));
3692+
3693+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3694+
3695+
validateJavaSourceFiles(files);
3696+
assertFileContains(
3697+
output.resolve("src/main/java/xyz/abcdef/ApiClient.java"),
3698+
"public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {",
3699+
"if (null == dateFormat) {",
3700+
"dateFormat = createDefaultDateFormat();"
3701+
);
3702+
}
3703+
}
3704+
36743705

36753706
@Test
36763707
public void testRestClientWithUseSingleRequestParameter_issue_19406() {

samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ public static DateFormat createDefaultDateFormat() {
134134
}
135135

136136
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
137+
if (null == dateFormat) {
138+
dateFormat = createDefaultDateFormat();
139+
}
137140
return JsonMapper.builder()
138141
.defaultDateFormat(dateFormat)
139142
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public static DateFormat createDefaultDateFormat() {
135135
}
136136

137137
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
138+
if (null == dateFormat) {
139+
dateFormat = createDefaultDateFormat();
140+
}
138141
return JsonMapper.builder()
139142
.defaultDateFormat(dateFormat)
140143
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public static DateFormat createDefaultDateFormat() {
148148
}
149149

150150
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
151+
if (null == dateFormat) {
152+
dateFormat = createDefaultDateFormat();
153+
}
151154
return JsonMapper.builder()
152155
.defaultDateFormat(dateFormat)
153156
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public static DateFormat createDefaultDateFormat() {
149149
}
150150

151151
public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
152+
if (null == dateFormat) {
153+
dateFormat = createDefaultDateFormat();
154+
}
152155
return JsonMapper.builder()
153156
.defaultDateFormat(dateFormat)
154157
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

0 commit comments

Comments
 (0)