Is there a suggested way how to deal with one stat...
# ktor
f
Is there a suggested way how to deal with one status code having multiple possible response bodies in the openapi generator? E.g. 400 having
data class TooManyParameters(val limit: Int = 0)
and
data class CustomReason(val reason: String)
Can I document the response twice and the generator merges it into a oneOf?
j
I think you need to use a sealed hierarchy for that to work, something like that:
Copy code
@Serializable
sealed interface BadRequestBody

@Serializable @SerialName("too-many-parameters")
data class TooManyParameters(val limit: Int = 0) : BadRequestBody

@Serializable @SerialName("custom-reason")
data class CustomReason(val reason: String) : BadRequestBody
a
To solve the problem, you can construct a
JsonSchema
explicitly with an
oneOf
. Here is an example:
Copy code
get("/400") {
    call.respond(HttpStatusCode.BadRequest)
}.describe {
    responses {
        HttpStatusCode.BadRequest {
            schema = JsonSchema(oneOf = listOf(
                ReferenceOr.Value(jsonSchema<TooManyParameters>()),
                ReferenceOr.Value(jsonSchema<CustomReason>()),
            ))
        }
    }
}
f
Yeah the sealed hierarchy thing i did for 403, but for 400 documenting all BadRequest responses for every route would not describe my routes very well.
I guess then I'll use the .describe api (the KDoc one just reads a bit nicer)