Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate openapi in ktor?

Tags:

kotlin

ktor

I'm writing some rstAPI with Ktor and want it to be autodocumentated.

I don't need swagger HTML generation and stuff, just raw openapi.yml

Searching for "ktor openapi generation" and etc gives me official docs link (https://ktor.io/docs/server-openapi.htm)

But, if I try to write same code in my project:

fun main() {
  embeddedServer(
    Netty,
    port = Config.load().main.port,
    host = Config.load().main.host,
    module = Application::module
  ).start(wait = true)
}



fun Application.module() {
  routing {

    route("/api") {
      get {
        call.respond("Hello world!")
      }
    }

    openAPI(path = "/api/openapi", swaggerFile = "documentation.yaml", )


  }
}

It just gives me FileNotFoundException

Exception in thread "main" java.io.FileNotFoundException: Swagger file not found: documentation.yaml
        at io.ktor.server.plugins.openapi.OpenAPIKt.readOpenAPIFile(OpenAPI.kt:57)
        at io.ktor.server.plugins.openapi.OpenAPIKt.openAPI(OpenAPI.kt:27)
        at io.ktor.server.plugins.openapi.OpenAPIKt.openAPI$default(OpenAPI.kt:22)
        at _4Tipsy.TinyCloudAPI.AppKt$module$1.invoke(App.kt:39)
        ...

As I got it, io.ktor:ktor-server-openapi wasn't made for openapi generation.

So, please, tell me about any solution.

like image 728
Kristiano Odadu Avatar asked Oct 30 '25 19:10

Kristiano Odadu


2 Answers

You can generate the OpenAPI specification for a Ktor server application with the official Ktor plugin built into the IntelliJ IDEA Ultimate. Alternatively, the specification can be generated with a third-party tool Kopendium, which allows documenting the server API through code.

like image 74
Aleksei Tirman Avatar answered Nov 03 '25 01:11

Aleksei Tirman


You can use the ktor-swagger-ui library by SMILEY4 to easily integrate Swagger UI into your Ktor application. This library allows you to automatically host a Swagger UI route for your defined API routes, enabling you to view and interact with your API documentation directly in the browser.

like image 39
dok Avatar answered Nov 03 '25 00:11

dok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!