File tree Expand file tree Collapse file tree
examples/src/main/kotlin/io/github/smiley4/ktoropenapi/examples
ktor-openapi/src/main/kotlin/io/github/smiley4/ktoropenapi/config Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package io.github.smiley4.ktoropenapi.examples
22
33import io.github.smiley4.ktoropenapi.OpenApi
4- import io.github.smiley4.ktoropenapi.config.ExampleEncoder
54import io.github.smiley4.ktoropenapi.config.descriptors.KTypeDescriptor
65import io.github.smiley4.ktoropenapi.get
76import 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"
Original file line number Diff line number Diff line change @@ -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
Original file line number Diff line number Diff line change 11package io.github.smiley4.ktoropenapi.config
22
3+ import com.fasterxml.jackson.core.type.TypeReference
34import com.fasterxml.jackson.databind.ObjectMapper
45import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
56import 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
You can’t perform that action at this time.
0 commit comments