Francisco Ripoli
01/03/2023, 3:45 PMSpring
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?Andreas Scheja
01/03/2023, 4:37 PMinterface MyApiController {
suspend fun myOperation(call: ApplicationCall)
}
one extension function for Route
acceping an implementation of said interface i.e.
fun Route.myApi(controller: MyApiController) {
route("/my-api/my-operation") {
post {
controller.myOperation(call)
}
}
}
• So our app only has to do the setup
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.