SMILEY4 / ktor-swagger-ui

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

How to generate documentation of a sealed class using kotlinx.serialization #84

Closed laurentthiebaudezm closed 4 months ago

laurentthiebaudezm commented 5 months ago

Hi,

We're using kotlinx.serialization in our project (and we'd like to not use Jackson on top of that). No doc is generated for sealed class like in https://github.com/SMILEY4/ktor-swagger-ui/issues/46.

The trick to annotate with @JsonSubTypes doesn't work for me.

install(SwaggerUI) {
                        info {
                            title = "Example API"
                            version = "latest"
                            description = "Example API for testing and demonstration purposes."
                        }
                        server {
                            description = "hello!"
                        }
}

get("hello", {
                        tags = listOf("test")
                        description = "Hello World Endpoint"
                        operationId = "hello"
                        response {
                            default {
                                description = "Default Response"
                            }
                            HttpStatusCode.OK to {
                                description = "Successful Request"
                                body<MyResponse> { description = "the response" }
                            }
                            HttpStatusCode.InternalServerError to {
                                description = "Something unexpected happened"
                            }
                            "Custom" to {
                                description = "Custom Response"
                            }
                        }
                    }) {
                        call.respondText("Hello World!")
                    }

...

@JsonSubTypes(
        JsonSubTypes.Type(value = ResponseA::class),
        JsonSubTypes.Type(value = ResponseB::class),
    )
    @Serializable
    sealed class MyResponse
    @Serializable
    data class ResponseA(val name: String): MyResponse()
    @Serializable
    data class ResponseB(val name: String): MyResponse()

Response schema is MyResponse{}

Any way to make it work?

SMILEY4 commented 5 months ago

Hi, I'm currently having some minor issues with the json-schema generator i am using, resulting in the jackson annotation (here JsonSubTypes) to be ignored. You can add this back and fix your bug in by adding:

EncodingData.DEFAULT_SCHEMA_GENERATOR = SchemaGenerator(
    EncodingData
        .schemaGeneratorConfigBuilder()
        .with(JacksonModule()) // add back JacksonModule
        .build()
)

install(SwaggerUI) { /*...*/ }

Just as a side-note: The json-schemas and examples are still generated using jackson not kotlinx. If you want to get rid of jackson completly, you can switch over to kotlinx.serialization. Check out the wiki or the example for more information. You should not need to change too much else, but the resulted schemas might be slightly different.