cjbooms / fabrikt

Generates Kotlin Code from OpenApi3 Specifications
Apache License 2.0
154 stars 40 forks source link

Ktor server controllers #214

Closed atollk closed 2 months ago

atollk commented 1 year ago

I haven't completely fleshed out this idea but just playing around with it, I feel like it shouldn't be too hard to add generation of a Ktor-based server as an option. And since afaik there are no Ktor-generators for OpenAPI in existence yet, it would probably be a pretty useful feature.

The idea would be to generate the same controller interfaces as for Spring or Micronaut right now, with the main difference that they don't use annotations but rather the Ktor DSL.

For example, Spring:

@Controller
@Validated
@RequestMapping("")
interface InternalEventsController {
    /**
     * Generate change events for a list of entities
     *
     * @param bulkEntityDetails
     */
    @RequestMapping(
        value = ["/internal/events"],
        produces = ["application/json", "application/problem+json"],
        method = [RequestMethod.POST],
        consumes = ["application/json"]
    )
    fun post(
        @RequestBody @Valid
        bulkEntityDetails: BulkEntityDetails
    ): ResponseEntity<EventResults>
}

Ktor:

interface InternalEventsController {
    /**
     * Generate change events for a list of entities
     *
     * @param bulkEntityDetails
     */
    fun post(
        bulkEntityDetails: BulkEntityDetails
    ): HttpResponse<EventResults>

    companion object {
        // needs to be called from somewhere
        fun addRouting(routing: Routing, controller: InternalEventsController) {
            routing.route("/internal/events") {
                post {
                    val request = call.receive<BulkEntityDetails>()
                    val response = controller.post(request)
                    call.respond(response)
                }
            }
        }
    }
}
ulrikandersen commented 5 months ago

Are you still interested in this feature @atollk?

I have been working on a first iteration and it would be great to get your feedback on it.

atollk commented 5 months ago

Hey. I personally do not need this feature anymore right now but I could still try and give some feedback if you want me to.

ulrikandersen commented 5 months ago

That would be great, @atollk. I'll ping you here when I have a PR ready.

ulrikandersen commented 5 months ago

@atollk please have a look at #280 when you have time 🙏