SMILEY4 / ktor-swagger-ui

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

Use @Description annotation on Request/Response body objects #130

Closed runningjustin closed 1 month ago

runningjustin commented 2 months ago

Perhaps there is another way to do this, but it doesn't seem like the Kernernator @Description annotation on the members in my data classes are being processed. For example:

@Serializable
data class TransactionData(
    @Description("The currency code for the transaction")
    val currency: String,
)

Shouldn't the description for currency appear in the API spec for any APIs that define a request or response body<TransactionData>?

As always, thank you! This is by FAR the best OpenAPI doc tool for Kotlin-based projects.

runningjustin commented 1 month ago

@SMILEY4 is this something you think you can support or should I look for other options?

SMILEY4 commented 1 month ago

Hi, sorry for the late response.

The io.github.smiley4.schemakenerator.core.annotations.Description annotation should be supported and included by default. I'm having issues reproducing the problem. This:

//...
import io.github.smiley4.schemakenerator.core.annotations.Description

fun main() {
    embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
}

private fun Application.myModule() {

    @Serializable
    data class TransactionData(
        @Description("The currency code for the transaction")
        val currency: String,
    )

    install(SwaggerUI)

    routing {
        route("swagger") { swaggerUI("/api.json") }
        route("api.json") { openApiSpec() }

        get("hello", {
            description = "A Hello-World route"
            request {
                body<TransactionData>()
            }
        }) {
            call.respondText("Hello World!")
        }

    }

}

... using the latest version (3.4.0) results in the following schema in the spec:

"io.github.smiley4.ktorswaggerui.examples.TestKt$myModule$TransactionData" : {
  "title" : "TransactionData",
  "required" : [ "currency" ],
  "type" : "object",
  "properties" : {
    "currency" : {
      "title" : "String",
      "type" : "string",
      "description" : "The currency code for the transaction"
    }
  }
}

Can you maybe provide an example to reproduce the problem or check if you are using a different plugin config?

runningjustin commented 1 month ago

Thank you! I am using a custom generator and it turns out I was missing a call to handleCoreAnnotations(). Here's what I ended up with:

schemas {
        // customized schema generation pipeline
        generator = { type ->
            type
                .processReflection() {
                    redirect<Instant, String>()
                    redirect<BigDecimal, String>()
                }
                .handleNameAnnotation()
                .handleJacksonAnnotations()
                .connectSubTypes() // connect the supertypes with their subtypes
                .generateSwaggerSchema()
                .handleCoreAnnotations()
                .withTitle(TitleType.OPENAPI_SIMPLE)
                .compileReferencingRoot()
        }
    }