When deploying my Ktor app to production (running ...
# ktor
r
When deploying my Ktor app to production (running a fat jar) I have an error about generating swagger supporting file. More in thread.
The exception is:
Copy code
Oct 13 19:07:18 prv-sodtest.finn.pl finn-java-jre21[1132819]: 2025-10-13 19:07:18,146 [main] WARN  i.s.c.v.i.CodegenIgnoreProcessor - Output directory does not exist, or is 
inaccessible. No file (.swagger-codegen-ignore) will be evaluated.
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]: 2025-10-13 19:07:19,335 [main] INFO  i.s.codegen.v3.AbstractGenerator - writing file docs/index.html
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]: Exception in thread "main" java.lang.RuntimeException: Could not generate supporting file 'SupportingFile:
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         templateFile: index.mustache
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         folder:
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         destinationFilename: index.html
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]: '
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         at io.swagger.codegen.v3.DefaultGenerator.generateSupportingFiles(DefaultGenerator.java:677)
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:860)
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         at io.ktor.server.plugins.openapi.OpenAPIKt.openAPI(OpenAPI.kt:45)
Oct 13 19:07:19 prv-sodtest.finn.pl finn-java-jre21[1132819]:         at io.ktor.server.plugins.openapi.OpenAPIKt.openAPI$default(OpenAPI.kt:24)
I'm using openapi and swagger plugins
Copy code
routing {
        openAPI(path = "openapi", swaggerFile = "api.yaml")
        swaggerUI(path = "swagger", swaggerFile = "api.yaml")
    }
When running the app on my local machine I see
docs
directory is generated in the current directory. I think there are no permissions to write files on production. Is this configurable? Can be disabled without removing the swagger plugin?
a
As I can see from the code, the
docs
directory is hardcoded. You can inline the implementation of the
openAPI
method, which is quite small, and change the directory there. Here is an example:
Copy code
fun readOpenAPIFile(swaggerFile: String, classLoader: ClassLoader): String {
    val resource = classLoader.getResourceAsStream(swaggerFile)
        ?.bufferedReader()?.readText()

    if (resource != null) return resource

    val file = File(swaggerFile)
    if (!file.exists()) {
        throw FileNotFoundException("Swagger file not found: $swaggerFile")
    }

    return file.readText()
}

fun main(args: Array<String>) {
    embeddedServer(Netty, port = 9000) {
        routing {
            val apiDocument = readOpenAPIFile("api.yaml", environment.classLoader)

            val config = OpenAPIConfig()
            with(config) {
                val swagger = parser.readContents(apiDocument, null, options)
                val dir = File("docs") // Change the dir
                dir.mkdirs()

                opts.apply {
                    config(codegen)
                    opts(ClientOpts())
                    openAPI(swagger.openAPI)
                }

                generator.opts(opts)
                generator.generate()

                staticFiles("openapi", dir)
            }
        }
    }.start(wait = true)
}
thank you color 1