papsign / Ktor-OpenAPI-Generator

Ktor OpenAPI/Swagger 3 Generator
Apache License 2.0
241 stars 42 forks source link

How to set operationId for endpoint? #39

Closed bargergo closed 4 years ago

bargergo commented 4 years ago

I would like to generate client code from the generated swagger doc and use the first Tag and the operationId for naming the methods. I have found in the tests how to add tags, but I can't find anywhere how to add operationId.

Wicpar commented 4 years ago

There is no module implemented for it at the moment, you can implement it like this:

// Convenience method, can be omitted
fun id(id: String? = null) = EndpointId(id)

// Must implement both interfaces, RouteOpenAPIModule to indicate that it can only be used on a single endpoint, OperationModule to hook into the Operation (the endpoint descriptor) generation 
data class EndpointId(val id: String? = null) : OperationModule, RouteOpenAPIModule {
    override fun configure(apiGen: OpenAPIGen, provider: ModuleProvider<*>, operation: OperationModel) {
        operation.operationId = id
    }
}

// use like this for instance, if you were to declare a GET request.
.get<YourParam, YourResponse>(
        id("my_endpoint") // declare your route id in the enpoint 'modules' vararg parameter
    ) {
    ...
}
bargergo commented 4 years ago

Thank you for your detailed help! I really appreciate it.

There is a typo in the EndPointId example: the type of operation should be Operation and not OperationModel, but aside from that, it works well. Thanks again!