Skip to content

Commit 7987487

Browse files
committed
feat: use jackson in jvm factory of kotlin client
1 parent 245ce91 commit 7987487

4 files changed

Lines changed: 45 additions & 9 deletions

File tree

pkg/generator/openapi-kotlin/generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (g *KotlinGenerator) ToCodeType(schema *base.Schema, schemaType openapigene
223223

224224
// multiple types (e.g., ["string", "null"])
225225
if util.CountExcluding(schema.Type, "null") > 1 {
226-
return openapigenerator.CodeType{Name: "JsonElement"}, nil
226+
return openapigenerator.CodeType{Name: "Any"}, nil
227227
}
228228

229229
switch {
@@ -312,11 +312,11 @@ func (g *KotlinGenerator) ToCodeType(schema *base.Schema, schemaType openapigene
312312
} else if schema.AdditionalProperties != nil && schema.AdditionalProperties.IsB() && schema.AdditionalProperties.B {
313313
return openapigenerator.NewMapCodeType(
314314
openapigenerator.NewSimpleCodeType("String", schema),
315-
openapigenerator.NewSimpleCodeType("JsonElement", schema),
315+
openapigenerator.NewSimpleCodeType("Any", schema),
316316
schema,
317317
), nil
318318
} else if schema.AdditionalProperties == nil && schema.Properties == nil {
319-
return openapigenerator.CodeType{Name: "JsonElement"}, nil
319+
return openapigenerator.CodeType{Name: "Any"}, nil
320320
} else {
321321
if schema.Title == "" {
322322
return openapigenerator.DefaultCodeType, fmt.Errorf("schema does not have a title. schema: %s", schema.Type)
@@ -336,7 +336,7 @@ func (g *KotlinGenerator) ToCodeType(schema *base.Schema, schemaType openapigene
336336
if openapigenerator.HaveSameCodeTypeName(codeTypes) {
337337
return codeTypes[0], nil
338338
}
339-
return openapigenerator.CodeType{Name: "JsonElement"}, nil
339+
return openapigenerator.CodeType{Name: "Any"}, nil
340340

341341
default:
342342
return openapigenerator.DefaultCodeType, fmt.Errorf("unhandled type. schema: %s, format: %s", schema.Type, schema.Format)

pkg/template/templates/openapi-kotlin-httpclient/api_factory.jvm.gohtml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ import io.ktor.client.plugins.HttpTimeout
2121
import io.ktor.client.plugins.DefaultRequest
2222
import io.ktor.client.plugins.logging.LogLevel
2323
import io.ktor.client.request.header
24+
import io.ktor.http.ContentType
25+
import io.ktor.http.HttpHeaders
2426
import io.ktor.serialization.kotlinx.json.*
2527
import io.ktor.serialization.kotlinx.xml.*
26-
import io.ktor.http.HttpHeaders
28+
import io.ktor.serialization.jackson3.JacksonConverter
29+
import io.ktor.serialization.jackson3.jackson
30+
2731
import io.opentelemetry.instrumentation.ktor.v3_0.KtorClientTelemetry
2832

2933
import kotlinx.coroutines.CoroutineScope
3034
import kotlinx.coroutines.Dispatchers
3135
import kotlinx.coroutines.SupervisorJob
3236
import kotlinx.serialization.json.Json
3337

38+
import tools.jackson.databind.DeserializationFeature
39+
import tools.jackson.databind.SerializationFeature
40+
import tools.jackson.module.kotlin.KotlinModule
41+
import tools.jackson.dataformat.xml.XmlMapper
42+
3443
import org.slf4j.LoggerFactory
3544

3645
object {{ .Metadata.Name }}Factory {
@@ -54,12 +63,31 @@ fun <T : Any> buildClient(api: KClass<T>, spec: Jvm{{ .Metadata.Name }}FactorySp
5463

5564
// content negotiation
5665
install(ContentNegotiation) {
66+
/*
5767
json(Json {
5868
prettyPrint = true
5969
isLenient = true
6070
ignoreUnknownKeys = true
6171
})
6272
xml()
73+
*/
74+
75+
jackson {
76+
enable(SerializationFeature.INDENT_OUTPUT)
77+
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // do not fail on new fields
78+
configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) // allow single values to be read as array
79+
configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false) // do not fail on invalid polymorphic sub types
80+
addModule(KotlinModule.Builder().build()) // kotlin support
81+
}
82+
register(
83+
ContentType.Application.Xml, JacksonConverter(
84+
XmlMapper.builder()
85+
.enable(SerializationFeature.INDENT_OUTPUT)
86+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // do not fail on new fields
87+
.addModule(KotlinModule.Builder().build()) // kotlin support
88+
.build()
89+
)
90+
)
6391
}
6492

6593
// retry policy

pkg/template/templates/openapi-kotlin-httpclient/build.gradle.kts.core.gohtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ kotlin {
4444
// ktor jvm engine
4545
api(libs.ktor.client.cio)
4646

47+
// serialization
48+
api(libs.ktor.serialization.jackson)
49+
api(project.dependencies.platform(libs.jackson.bom))
50+
api(libs.jackson.dataformat.xml)
51+
api(libs.jackson.dataformat.yaml)
52+
4753
// logging and tracing
4854
api(libs.opentelemetry.ktor)
4955

pkg/template/templates/openapi-kotlin-httpclient/libs.versions.toml.gohtml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{{- template "header-hash" }}
33

44
[versions]
5-
ktor-version = "3.4.1"
5+
jackson-version = "3.1.1"
6+
ktor-version = "3.4.2"
67
ktor-metrics-micrometer-version = "1.6.8"
78
kotlin-version = "2.3.20"
89
kotlinx-coroutines-version = "1.10.2"
@@ -12,9 +13,9 @@ opentelemetry-ktor-version = "2.26.1-alpha"
1213
jetbrains-annotations-version = "26.1.0"
1314

1415
[libraries]
15-
openfeign-bom = { module = "io.github.openfeign:feign-bom", version = "13.6" }
16-
resilience4j-bom = { module = "io.github.resilience4j:resilience4j-bom", version = "2.3.0" }
17-
jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version = "2.20.0" }
16+
jackson-bom = { module = "tools.jackson:jackson-bom", version.ref = "jackson-version" }
17+
jackson-dataformat-xml = { module = "tools.jackson.dataformat:jackson-dataformat-xml" }
18+
jackson-dataformat-yaml = { module = "tools.jackson.dataformat:jackson-dataformat-yaml" }
1819
micrometer-bom = { module = "io.micrometer:micrometer-bom", version = "1.15.5" }
1920
micrometer-core = { module = "io.micrometer:micrometer-core" }
2021
slf4j-bom = { module = "org.slf4j:slf4j-bom", version = "2.0.17" }
@@ -30,6 +31,7 @@ ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor-ve
3031
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor-version" }
3132
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor-version" }
3233
ktor-serialization-kotlinx-xml = { module = "io.ktor:ktor-serialization-kotlinx-xml", version.ref = "ktor-version" }
34+
ktor-serialization-jackson = { module = "io.ktor:ktor-serialization-jackson3", version.ref = "ktor-version" }
3335
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor-version" }
3436
ktor-client-tracing = { module = "io.ktor:ktor-client-tracing", version.ref = "ktor-version" }
3537
ktor-client-observer = { module = "io.ktor:ktor-client-observer", version.ref = "ktor-version" }

0 commit comments

Comments
 (0)