ktor icon indicating copy to clipboard operation
ktor copied to clipboard

Jackson-backed `ApplicationCall.receive` does not throw `ContentTransformationException`

Open lopcode opened this issue 6 years ago • 2 comments

Ktor Version

1.2.0

Ktor Engine Used(client or server and name)

CIO

JVM Version, Operating System and Relevant Context

JDK 9, macOS, io.ktor:ktor-jackson:1.2.0

Feedback

The docs (in code) suggest that when calling receive<T> to transform a request POST body to a data class type, if the content cannot be transformed, a ContentTransformationException will be thrown.

When using io.ktor:ktor-jackson:1.2.0, instead, a Jackson Kotlin exception is thrown of type JsonMappingException (or, specifically in my case, a MissingKotlinParameterException).

Here's some sample code that I've simplified slightly to echo the request body back, and it includes the use of a thin wrapper in my application (RequestHandling). The expected behaviour is that the user would get a HttpStatusCode.BadRequest back if they submitted a request that didn't include the password field (for example, { "email": "[email protected]" }). Instead, a MissingKotlinParameterException is unhandled, forcing a 500 response. The same happens for receieveOrNull.

class SignUpHandler: RequestHandling {

    data class SignUpRequest(val email: String, val password: String)

    override suspend fun handle(call: ApplicationCall) {
        val requestBody = try {
            call.receive<SignUpRequest>()
        } catch (exception: ContentTransformationException) {
            return call.respond(HttpStatusCode.BadRequest)
        }

        return call.respond(requestBody)
    }
}

Please let me know if you need any other details 👍

lopcode avatar May 22 '19 15:05 lopcode

this is a duplicate of #711

camhashemi avatar Jun 19 '19 14:06 camhashemi

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

oleg-larshin avatar Aug 10 '20 15:08 oleg-larshin