Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KTOR-7665 Jackson should respond with 500 when Jackson converter fails with exception #4507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.engine.internal.*
import io.ktor.server.http.*
import io.ktor.server.plugins.*
import io.ktor.server.response.*
import io.ktor.util.*
import io.ktor.util.cio.*
Expand Down Expand Up @@ -128,10 +127,20 @@ public abstract class BaseApplicationResponse(

// WriteChannelContent is more efficient than ReadChannelContent
is OutgoingContent.WriteChannelContent -> {
val ch = ByteChannel()
try {
// need to be in external function to keep tail suspend call
respondWriteChannelContent(ch, content)
} catch (cause: Throwable) {
status(HttpStatusCode.InternalServerError)
throw cause
}

// First set headers
commitHeaders(content)
// need to be in external function to keep tail suspend call
respondWriteChannelContent(content)
responseChannel().use {
ch.copyTo(this)
}
}

// Pipe is the least efficient
Expand Down Expand Up @@ -168,9 +177,9 @@ public abstract class BaseApplicationResponse(
/**
* Process response [content] using [OutgoingContent.WriteChannelContent.writeTo].
*/
protected open suspend fun respondWriteChannelContent(content: OutgoingContent.WriteChannelContent) {
protected open suspend fun respondWriteChannelContent(ch: ByteWriteChannel, content: OutgoingContent.WriteChannelContent) {
// Retrieve response channel, that might send out headers, so it should go after commitHeaders
responseChannel().use {
ch.use {
// Call user code to send data
// val before = totalBytesWritten
try {
Expand All @@ -179,6 +188,11 @@ public abstract class BaseApplicationResponse(
}
} catch (closed: ClosedWriteChannelException) {
throw ChannelWriteException(exception = closed)
} catch (cause: Exception) {
status(HttpStatusCode.InternalServerError)
println("EXCEPTION")
throw cause

}

// TODO currently we can't ensure length like that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.jackson.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlinx.coroutines.*
import java.io.*
import java.lang.reflect.*
import java.util.concurrent.*
import kotlin.coroutines.*
import kotlin.test.*
import java.io.Closeable
import java.lang.reflect.Method
import java.time.ZonedDateTime
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals

class ServerJacksonBlockingTest {
private val dispatcher = UnsafeDispatcher()
Expand Down Expand Up @@ -60,6 +64,29 @@ class ServerJacksonBlockingTest {
}
}

@Test
fun testJacksonModuleNotRegistered() = testApplication {
environment {
config = MapApplicationConfig("ktor.test.throwOnException" to "false")
}
install(ContentNegotiation) {
jackson()
}
routing {
get("/") {
call.respond(Message(msg = "Hi", time = ZonedDateTime.now()))
}
}

runBlocking {
client.get("/").apply {
assertEquals(HttpStatusCode.InternalServerError, status)
}
}
}

data class Message(val msg: String, val time: ZonedDateTime)

data class K(var i: Int)

private class UnsafeDispatcher : CoroutineDispatcher(), Closeable {
Expand Down