Skip to content

Commit e6a33d3

Browse files
committed
examples encoded as strings
1 parent 5029618 commit e6a33d3

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

examples/src/main/kotlin/io/github/smiley4/ktoropenapi/examples/Examples.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.smiley4.ktoropenapi.examples
22

33
import io.github.smiley4.ktoropenapi.OpenApi
4-
import io.github.smiley4.ktoropenapi.config.ExampleEncoder
54
import io.github.smiley4.ktoropenapi.config.descriptors.KTypeDescriptor
65
import io.github.smiley4.ktoropenapi.get
76
import io.github.smiley4.ktoropenapi.openApi
@@ -71,10 +70,10 @@ private fun Application.myModule() {
7170
body<MyExampleClass> {
7271
// specify two example values
7372
example("Example 1") {
74-
description = "A first example value"
75-
value = MyExampleClass(
76-
someValue = "example 1"
77-
)
73+
description = "A first example value"
74+
value = MyExampleClass(
75+
someValue = "example 1"
76+
)
7877
}
7978
example("Example 2") {
8079
description = "A second example value"

examples/src/main/kotlin/io/github/smiley4/ktoropenapi/examples/KotlinxSerialization.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlinx.serialization.json.Json
2323
import kotlinx.serialization.json.JsonNamingStrategy
2424

2525
fun main() {
26-
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
26+
embeddedServer(Netty, port = 8088, host = "localhost", module = Application::myModule).start(wait = true)
2727
}
2828

2929
private fun Application.myModule() {
@@ -42,7 +42,7 @@ private fun Application.myModule() {
4242
}
4343
examples {
4444
// configure the example encoder to encode kotlin objects using kotlinx-serializer
45-
exampleEncoder = ExampleEncoder.kotlinx(json)
45+
exampleEncoder = ExampleEncoder.jackson()
4646
}
4747
}
4848

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

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

3+
import com.fasterxml.jackson.core.type.TypeReference
34
import com.fasterxml.jackson.databind.ObjectMapper
45
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
56
import io.github.smiley4.ktoropenapi.config.descriptors.KTypeDescriptor
@@ -29,7 +30,7 @@ object ExampleEncoder {
2930
*/
3031
fun jackson(json: ObjectMapper? = null): GenericExampleEncoder = { type, example ->
3132
val mapper = json ?: jacksonObjectMapper()
32-
mapper.writeValueAsString(example)
33+
mapper.writeValueAsString(example).toStructuredJsonObject()
3334
}
3435

3536
/**
@@ -41,11 +42,13 @@ object ExampleEncoder {
4142
is KTypeDescriptor -> {
4243
val jsonEncoder = json ?: Json
4344
val serializer = jsonEncoder.serializersModule.serializer(type.type)
44-
jsonEncoder.encodeToString(serializer, example)
45+
jsonEncoder.encodeToString(serializer, example).toStructuredJsonObject()
4546
}
4647
else -> example
4748
}
4849
}
4950

51+
fun String.toStructuredJsonObject() = jacksonObjectMapper().readValue(this, object : TypeReference<Any>() {})
52+
5053
}
5154

0 commit comments

Comments
 (0)