Skip to content

Commit b61a616

Browse files
committed
fix(kotlin-client): use Jackson 3 builder API in Serializer template
Jackson 3 ObjectMapper is immutable — no more chaining .configure() and .setSerializationInclusion(). Use jsonMapper {} builder DSL with: - changeDefaultPropertyInclusion for NON_ABSENT inclusion - enable/disable for DateTimeFeature, EnumFeature, DeserializationFeature - addModule(kotlinModule()) instead of jacksonObjectMapper()
1 parent 31cf1a4 commit b61a616

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

modules/openapi-generator/src/main/resources/kotlin-client/jvm-common/infrastructure/Serializer.kt.mustache

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@ import kotlinx.datetime.LocalTime
3030
import java.util.UUID
3131
{{/gson}}
3232
{{#jackson}}
33+
{{^useJackson3}}
3334
import {{jacksonPackage}}.databind.DeserializationFeature
3435
import {{jacksonPackage}}.databind.ObjectMapper
35-
{{^useJackson3}}
3636
import {{jacksonPackage}}.databind.SerializationFeature
37+
import com.fasterxml.jackson.annotation.JsonInclude
38+
import {{jacksonPackage}}.module.kotlin.jacksonObjectMapper
3739
{{/useJackson3}}
3840
{{#useJackson3}}
41+
import {{jacksonPackage}}.databind.DeserializationFeature
42+
import {{jacksonPackage}}.databind.ObjectMapper
3943
import {{jacksonPackage}}.databind.cfg.DateTimeFeature
4044
{{#enumUnknownDefaultCase}}
4145
import {{jacksonPackage}}.databind.cfg.EnumFeature
4246
{{/enumUnknownDefaultCase}}
43-
{{/useJackson3}}
4447
import com.fasterxml.jackson.annotation.JsonInclude
45-
import {{jacksonPackage}}.module.kotlin.jacksonObjectMapper
48+
import {{jacksonPackage}}.module.kotlin.jsonMapper
49+
import {{jacksonPackage}}.module.kotlin.kotlinModule
50+
{{/useJackson3}}
4651
{{/jackson}}
4752
{{#kotlinx_serialization}}
4853
import java.math.BigDecimal
@@ -128,27 +133,34 @@ import java.util.concurrent.atomic.AtomicLong
128133
}
129134
{{/gson}}
130135
{{#jackson}}
136+
{{^useJackson3}}
131137
@JvmStatic
132138
val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
133-
{{^useJackson3}}
134139
.findAndRegisterModules()
135-
{{/useJackson3}}
136140
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
137141
{{#enumUnknownDefaultCase}}
138-
{{#useJackson3}}
139-
.configure(EnumFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
140-
{{/useJackson3}}
141-
{{^useJackson3}}
142142
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
143-
{{/useJackson3}}
144143
{{/enumUnknownDefaultCase}}
145-
{{#useJackson3}}
146-
.configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, false)
147-
{{/useJackson3}}
148-
{{^useJackson3}}
149144
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
150-
{{/useJackson3}}
151145
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, {{failOnUnknownProperties}})
146+
{{/useJackson3}}
147+
{{#useJackson3}}
148+
@JvmStatic
149+
val jacksonObjectMapper: ObjectMapper = jsonMapper {
150+
addModule(kotlinModule())
151+
changeDefaultPropertyInclusion { it.withValueInclusion(JsonInclude.Include.NON_ABSENT) }
152+
{{#enumUnknownDefaultCase}}
153+
enable(EnumFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
154+
{{/enumUnknownDefaultCase}}
155+
disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
156+
{{#failOnUnknownProperties}}
157+
enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
158+
{{/failOnUnknownProperties}}
159+
{{^failOnUnknownProperties}}
160+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
161+
{{/failOnUnknownProperties}}
162+
}
163+
{{/useJackson3}}
152164
{{/jackson}}
153165
{{#kotlinx_serialization}}
154166
private var isAdaptersInitialized = false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ public void shouldGenerateJackson3Imports() throws IOException {
10011001

10021002
Path serializerPath = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt");
10031003
TestUtils.assertFileContains(serializerPath, "import tools.jackson.databind.ObjectMapper");
1004-
TestUtils.assertFileContains(serializerPath, "import tools.jackson.module.kotlin.jacksonObjectMapper");
1004+
TestUtils.assertFileContains(serializerPath, "import tools.jackson.module.kotlin.jsonMapper");
10051005
TestUtils.assertFileNotContains(serializerPath, "com.fasterxml.jackson.databind");
10061006
}
10071007

samples/client/petstore/kotlin-jackson3/gradlew

100644100755
File mode changed.

samples/client/petstore/kotlin-jackson3/src/main/kotlin/org/openapitools/client/infrastructure/Serializer.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import tools.jackson.databind.ObjectMapper
55
import tools.jackson.databind.cfg.DateTimeFeature
66
import tools.jackson.databind.cfg.EnumFeature
77
import com.fasterxml.jackson.annotation.JsonInclude
8-
import tools.jackson.module.kotlin.jacksonObjectMapper
8+
import tools.jackson.module.kotlin.jsonMapper
9+
import tools.jackson.module.kotlin.kotlinModule
910

1011
object Serializer {
1112
@JvmStatic
12-
val jacksonObjectMapper: ObjectMapper = jacksonObjectMapper()
13-
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT)
14-
.configure(EnumFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
15-
.configure(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS, false)
16-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
13+
val jacksonObjectMapper: ObjectMapper = jsonMapper {
14+
addModule(kotlinModule())
15+
changeDefaultPropertyInclusion { it.withValueInclusion(JsonInclude.Include.NON_ABSENT) }
16+
enable(EnumFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
17+
disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
18+
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
19+
}
1720
}

0 commit comments

Comments
 (0)