hello all! On my previous `Spring` projects, I had...
# ktor
f
hello all! On my previous
Spring
projects, I had a separate maven project, just with a openapi yaml file, which I called the spec, and generated a jar file that would later be used on the actual service project, that way, both backend and fronted used that as a contract and we would not have issues with mismatching urls and request/response properties. I used to generate all that from my openapi yaml file using the openapi generator maven plugin … I’m trying to achieve something similar for
ktor
but it seems impossible. I’ve tried many combinations of the settings for the plugin, but no luck. Is anyone doing something similar?
a
We've also found the default ktor-server generator lacking for such a use-case (also it still uses ktor 1.x), so we've written a custom openapi generator that only generates the DTOs (essentially utilizing the default kotlin generator) and some routing utility per tag: • one interface providing the operations for the api i.e.
Copy code
interface MyApiController {
    suspend fun myOperation(call: ApplicationCall)
}
one extension function for
Route
acceping an implementation of said interface i.e.
Copy code
fun Route.myApi(controller: MyApiController) {
    route("/my-api/my-operation") {
         post {
                 controller.myOperation(call)
         }
    }
}
• So our app only has to do the setup
Copy code
routing {
    route("/base-route") {
        myApi(myApiControllerImplementation)
    }
}
This approach of course doesn't cover the actual request body/response body, but at least the urls are covered automatically. We did it like that to have the ability to just e.g.
return call.respond(HttpStatusCode.BadRequest)
in your methods and have our unit tests verify that the object passed to
call.respond()
matches the expected type.