Skip to content

Commit

Permalink
KTOR-5479 Improve log message about skipping response body transforma… (
Browse files Browse the repository at this point in the history
#3379)

* KTOR-5479 Improve log message about skipping response body transformation in the ContentNegotiation
  • Loading branch information
Stexxe authored Jan 30, 2023
1 parent 9bb3ec7 commit aaea969
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.server.plugins.contentnegotiation

import ch.qos.logback.classic.*
import ch.qos.logback.classic.spi.*
import ch.qos.logback.core.read.ListAppender
import io.ktor.client.request.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import kotlin.test.*

class LogTest {
@Test
fun ignoredTypeMessageForResponseBody() {
val logger = LOGGER as Logger
logger.level = Level.ALL

val listAppender = ListAppender<ILoggingEvent>()
logger.addAppender(listAppender)
listAppender.start()

val plugin = createRouteScopedPlugin("PartialContentNegotiation", ::ContentNegotiationConfig) {
convertResponseBody()
}

testApplication {
application {
install(plugin)
routing {
get("/") {
call.respond("test")
}
}
}

client.get("/")

listAppender.stop()
assertEquals(
"Skipping response body transformation from String to OutgoingContent for the GET / request" +
" because the String type is ignored. See [ContentNegotiationConfig::ignoreType].",
listAppender.list[0].message
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.ktor.http.content.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.http.content.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.utils.io.charsets.*

Expand All @@ -21,7 +22,13 @@ internal fun PluginBuilder<ContentNegotiationConfig>.convertResponseBody() = onC
}

if (pluginConfig.ignoredTypes.any { it.isInstance(subject) }) {
LOGGER.trace("Skipping because the type is ignored.")
val sourceClass = subject::class.simpleName
val requestInfo = "${call.request.httpMethod.value} ${call.request.uri}"

LOGGER.trace(
"Skipping response body transformation from $sourceClass to OutgoingContent for the $requestInfo request" +
" because the $sourceClass type is ignored. See [ContentNegotiationConfig::ignoreType]."
)
return@onCallRespond
}

Expand Down

0 comments on commit aaea969

Please sign in to comment.