3

I have a problem, I have already generated OpenApi Swagger files (swagger.json and swagger.yaml) Is it possible somehow in Ktor (kotlin) import this files and start swagger-ui server on specific route ? I have visited ktor-swagger project but could get how just to add json file to display swagger-ui. Any suggestions ?

1 Answers1

7

Make you json file accessible from static routing

  1. Create "files" directory in resources dir

  2. Use next code to routing static files from "files" dir

    routing {
        static("/static") {
            resources("files")
        }
        //...
    }
    
  3. Lets name json file "test.json", then put you it in "files" folder

so now you run the app and see this file by http://localhost:8080/static/test.json

Then we need to install and configure OpenApiGen

  1. Add OpenApiGen dependencies you can find how to do this here https://github.com/papsign/Ktor-OpenAPI-Generator

  2. Then use the following code

    install(OpenAPIGen) {
        serveSwaggerUi = true
        swaggerUiPath = "/swagger-ui"
    }
    

now you can use http://localhost:8080/swagger-ui/index.html?url=/static/test.json URL to see your file through the swagger UI

Also, you can provide your own route to using redirection

get("/api") {
   call.respondRedirect("/swagger-ui/index.html?url=/static/test.json", true)
}

this will allow you to use just http://localhost:8080/api

Cube
  • 362
  • 4
  • 8