papsign / Ktor-OpenAPI-Generator

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

Can't respond with custom status dynamically #23

Closed SerVB closed 4 years ago

SerVB commented 4 years ago

Hi! I don't understand how to respond with a custom status code, for example, 404, depending on the situation.

I try the following: I declare an interface and set it as a response type. Then I create some children of the interface with specified status codes. Example:


interface IResponse {

    val myField: Boolean
}

@Response(statusCode = 200)
object MyGoodResponse : IResponse {

    override val myField = true
}

@Response(statusCode = 404)
object MyBadResponse : IResponse {

    override val myField = false
}

// ...

route("a") {
    get<Unit, IResponse> {
        if (System.currentTimeMillis() % 2 == 0L) {
            respond(MyGoodResponse)
        } else {
            respond(MyBadResponse)
        }
    }
}

I want to see 404, but it's 200:

image

Is it supported now?

If you want to take a look a real-world example, please fresh start this revision of my project and call GET http://localhost:8080/v1/product/0, the status should be 404 but it returns OK and this test fails: https://github.com/SerVB/e-shop/blob/37958c8bb4fee8a42dad229c578f79867c1b5d74/server/src/test/kotlin/io/github/servb/eShop/product/route/singleOperation/EShopProductReturnProductTest.kt .

Wicpar commented 4 years ago

@Response is resolved during initialisation (No reflection during runtime in this library) on the generic type you specify in the generic parameters. Here it is IResponse which is not annotated. The way to handle this is to respond the goodresponse directly or throw a NotFound exception that you catch with a throw handler on the route that responds 404.

Exceptions are the design choice because exceptions are more maintainable if you have endpoints who call services that can fail. The services then just have to throw a handled exception without having to worry about responding it properly, which reduces the possibility of bugs in the long run.