tabilzad / ktor-docs-plugin

Provides Ktor Swagger support
34 stars 6 forks source link

Document generation ignoring methods with return type #36

Closed fyam1997 closed 1 day ago

fyam1997 commented 2 days ago

here is an example project for this issue https://github.com/fyam1997/KtorDocTest

route method with return type was ignored when generating document. In example below, blockBodyWithReturn and expressBody was ignored

@GenerateOpenApi
fun Route.rootRoute() {
    route("/root") {
        routeExpression()
        routeBlockBodyWithReturn()
        routeNoReturnType()
        routeExplicitUnit()
    }
}

fun Route.routeExpression(): Route = route("/expressBody") {
    get {}
}

fun Route.routeBlockBodyWithReturn(): Route {
    return route("/blockBodyWithReturn") {
        get {}
    }
}

fun Route.routeNoReturnType() {
    route("/noReturnType") {
        get {}
    }
}

@Suppress("RedundantUnitReturnType")
fun Route.routeExplicitUnit(): Unit {
    route("/explicitUnit") {
        get {}
    }
}

and output yaml would be

---
openapi: "3.1.0"
info:
  title: "Open API Specification"
  description: "Generated using Ktor Docs Plugin"
  version: "1.0.0"
paths:
  /root/noReturnType:
    get: {}
  /root/explicitUnit:
    get: {}
components:
  schemas: {}
fyam1997 commented 2 days ago

found a solution, just add this in ExpressionsVisitorK2

    override fun visitReturnExpression(returnExpression: FirReturnExpression, data: KtorElement?): List<KtorElement> {
        val funCall = returnExpression.result as? FirFunctionCall
        funCall?.accept(this, data)
        return super.visitReturnExpression(returnExpression, data)
    }