Skip to content

Commit 26038e7

Browse files
committed
add kotlin jackson example encoder
1 parent 65a7608 commit 26038e7

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/config/ExampleEncoder.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.smiley4.ktoropenapi.config
22

3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
35
import io.github.smiley4.ktoropenapi.config.descriptors.KTypeDescriptor
46
import io.github.smiley4.ktoropenapi.config.descriptors.TypeDescriptor
57
import kotlinx.serialization.json.Json
@@ -21,9 +23,18 @@ object ExampleEncoder {
2123
example
2224
}
2325

26+
/**
27+
* [GenericExampleEncoder] using a Jackson mapper to encode example objects.
28+
* @param json a jackson object mapper encoding objects to json. Set `null` to use a pre-configured object mapper.
29+
*/
30+
fun jackson(json: ObjectMapper? = null): GenericExampleEncoder = { type, example ->
31+
val mapper = json ?: jacksonObjectMapper()
32+
mapper.writeValueAsString(example)
33+
}
34+
2435
/**
2536
* [GenericExampleEncoder] using kotlinx-serialization to encode example objects.
26-
* @param json the kotlinx json serializer to use for encoding objects to json. Set `null` to use default kotlinx json serializer.
37+
* @param json a kotlinx json serializer to use for encoding objects to json. Set `null` to use default kotlinx json serializer.
2738
*/
2839
fun kotlinx(json: Json? = null): GenericExampleEncoder = { type, example ->
2940
when(type) {

ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/data/ExampleConfigData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class ExampleConfigData(
1414
val DEFAULT = ExampleConfigData(
1515
sharedExamples = emptyMap(),
1616
securityExamples = null,
17-
exampleEncoder = ExampleEncoder.internal()
17+
exampleEncoder = ExampleEncoder.jackson()
1818
)
1919
}
2020

0 commit comments

Comments
 (0)