ktorio / ktorio/ktor

Ktor Jackson: when sending empty content and using receive with type an exception occurs

Open
#711 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
14.5k
Forks
1.3k
Avg merge
2d 13h
Merged PRs (30d)
49

Description

I found an error in ktor feature using jackson. It does not handle empty body content very well.

I have this example application:

```
data class Han(val x: Int)

fun main(args: Array) {
val server = embeddedServer(Netty, port = 8080) {
install(ContentNegotiation) {
jackson {
propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE
}
}

routing {
post("/demo") {
val t = call.receive(Han::class)
}
}
}
server.start(wait = true)
}
```

When i send with postman an empty body with contentType: `application/json` to `/demo` it crashes with the following exception:

```
com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: (InputStreamReader); line: 1, column: 0]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4133)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3988)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3037)
at io.ktor.jackson.JacksonConverter.convertForReceive(JacksonConverter.kt:40)
at io.ktor.features.ContentNegotiation$Feature$install$3.doResume(ContentNegotiation.kt:95)
at io.ktor.features.ContentNegotiation$Feature$install$3.invoke(ContentNegotiation.kt)
at io.ktor.features.ContentNegotiation$Feature$instal...
```

I think this should be handled more gracefully than this hard exception.

Thank you guys for all your effort! Ktor is awesome.

Versions:
```
implementation("io.ktor:ktor-server-netty:0.9.5")
implementation("io.ktor:ktor-server-core:0.9.5")
implementation("io.ktor:ktor-jackson:0.9.5")
```
with kotlin `1.2.71`

Contributor guide

Open the contributing guide

Research direction

Start with io/ktor/jackson/JacksonConverter.kt at convertForReceive and reproduce the empty application/json request from the example against POST /demo. Check how ContentNegotiation.kt handles the converter result and define a test for the empty-body case; done means it is handled gracefully rather than producing the shown Jackson exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.