Skip to content

Commit e8453d3

Browse files
also fix Mapper in JSON.java for Jackson 3
1 parent a1e24d1 commit e8453d3

2 files changed

Lines changed: 25 additions & 22 deletions

File tree

  • modules/openapi-generator/src/main/resources/Java/libraries/native
  • samples/client/petstore/java/native-jackson3/src/main/java/org/openapitools/client

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import com.fasterxml.jackson.datatype.joda.JodaModule;
1717
{{#useJackson3}}
1818
import com.fasterxml.jackson.annotation.*;
1919
import tools.jackson.databind.*;
20+
import tools.jackson.databind.cfg.DateTimeFeature;
21+
import tools.jackson.databind.cfg.EnumFeature;
2022
import tools.jackson.databind.json.JsonMapper;
2123
{{#joda}}
2224
import tools.jackson.datatype.joda.JodaModule;
@@ -64,26 +66,23 @@ public class JSON {
6466
{{/openApiNullable}}
6567
{{/useJackson3}}
6668
{{#useJackson3}}
67-
mapper = JsonMapper.builder()
68-
.serializationInclusion(JsonInclude.Include.NON_NULL)
69-
// Note: MapperFeature.ALLOW_COERCION_OF_SCALARS was removed in Jackson 3
69+
JsonMapper.Builder jsonMapperBuilder = JsonMapper.builder()
70+
.changeDefaultPropertyInclusion(v -> v.withValueInclusion(JsonInclude.Include.NON_NULL))
7071
{{#failOnUnknownProperties}}
7172
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
7273
{{/failOnUnknownProperties}}
7374
{{^failOnUnknownProperties}}
7475
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
7576
{{/failOnUnknownProperties}}
7677
.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)
77-
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
78-
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
79-
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
80-
.defaultDateFormat(new RFC3339DateFormat())
81-
// Note: JavaTimeModule (jsr310) is built into jackson-databind for Jackson 3 - no explicit registration needed
82-
.build();
78+
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
79+
.enable(EnumFeature.WRITE_ENUMS_USING_TO_STRING)
80+
.enable(EnumFeature.READ_ENUMS_USING_TO_STRING)
81+
.defaultDateFormat(new RFC3339DateFormat());
8382
{{#joda}}
84-
mapper.registerModule(new JodaModule());
83+
jsonMapperBuilder.addModule(new JodaModule());
8584
{{/joda}}
86-
// FIXME: JsonNullableModule is not yet available for Jackson 3
85+
mapper = jsonMapperBuilder.build();
8786
{{/useJackson3}}
8887
}
8988

@@ -93,7 +92,12 @@ public class JSON {
9392
* @param dateFormat Date format
9493
*/
9594
public void setDateFormat(DateFormat dateFormat) {
95+
{{^useJackson3}}
9696
mapper.setDateFormat(dateFormat);
97+
{{/useJackson3}}
98+
{{#useJackson3}}
99+
mapper = ((JsonMapper) mapper).rebuild().defaultDateFormat(dateFormat).build();
100+
{{/useJackson3}}
97101
}
98102

99103
/**

samples/client/petstore/java/native-jackson3/src/main/java/org/openapitools/client/JSON.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import com.fasterxml.jackson.annotation.*;
1717
import tools.jackson.databind.*;
18+
import tools.jackson.databind.cfg.DateTimeFeature;
19+
import tools.jackson.databind.cfg.EnumFeature;
1820
import tools.jackson.databind.json.JsonMapper;
1921
import org.openapitools.client.model.*;
2022

@@ -29,18 +31,15 @@ public class JSON {
2931
private ObjectMapper mapper;
3032

3133
public JSON() {
32-
mapper = JsonMapper.builder()
33-
.serializationInclusion(JsonInclude.Include.NON_NULL)
34-
// Note: MapperFeature.ALLOW_COERCION_OF_SCALARS was removed in Jackson 3
34+
JsonMapper.Builder jsonMapperBuilder = JsonMapper.builder()
35+
.changeDefaultPropertyInclusion(v -> v.withValueInclusion(JsonInclude.Include.NON_NULL))
3536
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
3637
.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)
37-
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
38-
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
39-
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
40-
.defaultDateFormat(new RFC3339DateFormat())
41-
// Note: JavaTimeModule (jsr310) is built into jackson-databind for Jackson 3 - no explicit registration needed
42-
.build();
43-
// FIXME: JsonNullableModule is not yet available for Jackson 3
38+
.disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
39+
.enable(EnumFeature.WRITE_ENUMS_USING_TO_STRING)
40+
.enable(EnumFeature.READ_ENUMS_USING_TO_STRING)
41+
.defaultDateFormat(new RFC3339DateFormat());
42+
mapper = jsonMapperBuilder.build();
4443
}
4544

4645
/**
@@ -49,7 +48,7 @@ public JSON() {
4948
* @param dateFormat Date format
5049
*/
5150
public void setDateFormat(DateFormat dateFormat) {
52-
mapper.setDateFormat(dateFormat);
51+
mapper = ((JsonMapper) mapper).rebuild().defaultDateFormat(dateFormat).build();
5352
}
5453

5554
/**

0 commit comments

Comments
 (0)