When trying to generate server-side stubs for ktor from openapi, the output to me does not really look very usable.
I set up an example project on Github, where the setup can be reviewed. As I need to use maven in my real project, I used it here as well but I guess this should not make a difference to other generation methods.
The relevant part of the POM is this:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
What will be generated is the following:
- Data Model
- A
Paths.kt
object containing the paths to my routes in ktor-locations format - An example stub implementing the API as an extension function of
io.ktor.routing.Route
- A complete application, including configs, Dockerfile, Gradle Files, etc.
Coming from SpringBoot and Swagger, I am a bit confused, as this set of files is not really useful if you want to create an actual application implementing real business logic. I would have expected some default implementation holding single methods that I just need to implement / override. I realize that ktor has no DI built in, so I would also expect to have to extend the routing
stub in my application somehow.
However, with this set of generated code, I can really only use the data model and the Paths.kt
object. I will have my own application and the default implementation I can not override but need to rewrite completely myself.
So my question is, how to set up a proper ktor application implementing openapi specifications. Do I miss anything or do I really only get the model and Paths.kt
to work with?
Edit
For more clarity: especially with these parts of my application I am not happy to have no support from the tooling:
fun Application.module(testing: Boolean = false) {
// other setup
routing {
// some other interfaces I may have
FooApiImpl()
}
}
/**
* This Api Implementation has to be written completely manually. No help from the generated Code...
*/
@KtorExperimentalLocationsAPI
fun Route.FooApiImpl() {
get<Paths.getAllFoos> {
call.respond(listOf(
Foo( id = -1, bar = "hello"),
Foo( id = -2, bar = "world")
))
}
// the path gets named after the first method, which is weird in my eyes.
post<Paths.getAllFoos> {
val foo = call.receive<Foo>()
// fooService.save(foo)
call.respond(foo)
}
delete<Paths.deleteFooById> { path ->
val id = path.id
// fooService.deleteById(id)
call.respond(HttpStatusCode.NoContent)
}
}