SMILEY4 / ktor-swagger-ui

Kotlin Ktor plugin to generate OpenAPI and provide Swagger UI
Apache License 2.0
156 stars 25 forks source link

Is it possible to use SerialName in data class fields? #48

Closed pdrozz closed 1 year ago

pdrozz commented 1 year ago

Is there any way to use a kotlinx.serialization or gson annotation on the fields of a data class in the version 1.6.1?

For example, I'm using the code below, but the generated example response is picking the data class field name and ignoring the annotation:

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Pet(
   @SerialName("first_name") 
   val firstName: String
)

get("pet", {
    response {
        HttpStatusCode.Ok to {
            body<Pet>() {
               mediaType(ContentType.Application.Json)
               example(
                   name = "get pet name",
                   value = Pet("Max")
              )
            }
        }
    }
}) {
    // handle request...
}

But the generated example give me:

{
    "firstName": "Max"
}

instead

{
    "first_name": "Max"
}

I saw that in the 2.0.0 version will be possible to set a custom example encoding

SMILEY4 commented 1 year ago

Hi, there is this workaround for using kotlinx for schemas. For examples it is possible to set the value to a json-string provided by kotlinx instead of an object.

Version 2.x will have a much simpler/cleaner way of replacing the default encoders with e.g. kotlinx and should be done/release in a few days. If you want you could also check out the latest release candidate.