OpenAPI
Ktor plugin to automatically generate OpenAPI specifications from routes. Additional information can be gradually added to existing routes without requiring major changes to existing code.
Features
- Extends existing Ktor DSL
- No immediate change to code required
- Support for Type-safe routing / Resources plugin
- Document webhooks and (limited) options for server-sent events
- Covers (almost) complete OpenAPI 3.1.0 Specification
- Automatically generates json schemas from kotlin types
- Out-of-the-box support for type parameters, inheritance, collections, etc
- Usable with reflection or kotlinx.serialization
- Supports Jackson, Swagger, Javax and Jakarta annotations
- Highly configurable and customizable
Example
install(OpenApi) //(1)!
routing {
route("api.json") {
openApi() //(2)!
}
get("example", {
description = "An example route" //(3)!
response {
HttpStatusCode.OK to {
description = "A success response"
body<String>()
}
}
}) {
call.respondText("Hello World!") //(4)!
}
}
- Install and configure the OpenAPI plugin.
- Create a route to expose the OpenAPI specification file at
/api.json
. - Add (optional) information to the route, e.g. a description and responses and response bodies.
- Handle requests as usual.