Skip to content
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
@@ -0,0 +1,33 @@
/*
* Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import io.ktor.serialization.gson.GsonConverter
import io.ktor.util.reflect.typeInfo
import io.ktor.utils.io.ByteChannel
import io.ktor.utils.io.charsets.Charsets
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertNull
import kotlin.time.Duration.Companion.milliseconds

@Serializable
data class Payload(val value: String)

class ConverterTest {
@Test
fun returnsNullForEmptyChannelWithDelayedClose() = runTest {
val channel = ByteChannel()
launch {
delay(200.milliseconds)
channel.close()
}

val converter = GsonConverter()
val result = converter.deserialize(Charsets.UTF_8, typeInfo<Payload>(), channel)
assertNull(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import com.fasterxml.jackson.databind.exc.MismatchedInputException
import io.ktor.serialization.JsonConvertException
import io.ktor.serialization.jackson.JacksonConverter
import io.ktor.util.reflect.typeInfo
import io.ktor.utils.io.ByteChannel
import io.ktor.utils.io.charsets.Charsets
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertIs
import kotlin.time.Duration.Companion.milliseconds

@Serializable
data class Payload(val value: String)

class ConverterTest {
@Test
fun throwsExceptionForEmptyChannel() = runTest {
val channel = ByteChannel()
launch {
delay(200.milliseconds)
channel.close()
}

val converter = JacksonConverter()
val cause = assertFailsWith<JsonConvertException> {
converter.deserialize(Charsets.UTF_8, typeInfo<Payload>(), channel)
}

assertIs<MismatchedInputException>(cause.cause)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import io.ktor.serialization.JsonConvertException
import io.ktor.serialization.jackson3.JacksonConverter
import io.ktor.util.reflect.typeInfo
import io.ktor.utils.io.ByteChannel
import io.ktor.utils.io.charsets.Charsets
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import tools.jackson.databind.exc.MismatchedInputException
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertIs
import kotlin.time.Duration.Companion.milliseconds

@Serializable
data class Payload(val value: String)

class ConverterTest {
@Test
fun throwsExceptionForEmptyChannel() = runTest {
val channel = ByteChannel()
launch {
delay(200.milliseconds)
channel.close()
}

val converter = JacksonConverter()
val cause = assertFailsWith<JsonConvertException> {
converter.deserialize(Charsets.UTF_8, typeInfo<Payload>(), channel)
}

assertIs<MismatchedInputException>(cause.cause)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ kotlin {
api(projects.ktorSerialization)
api(libs.kotlinx.serialization.core)
}
commonTest.dependencies {
implementation(projects.ktorSerializationKotlinxJson)
implementation(libs.kotlinx.coroutines.test)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ package io.ktor.serialization.kotlinx
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.serialization.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.flow.*
import kotlinx.io.*
import kotlinx.serialization.*
import kotlin.jvm.*

/**
* Creates a converter serializing with the specified string [format]
Expand Down Expand Up @@ -58,13 +55,14 @@ public class KotlinxSerializationConverter(
}

override suspend fun deserialize(charset: Charset, typeInfo: TypeInfo, content: ByteReadChannel): Any? {
val fromExtension = extensions.asFlow()
.map { it.deserialize(charset, typeInfo, content) }
.firstOrNull { it != null || content.isClosedForRead }
if (extensions.isNotEmpty() && (fromExtension != null || content.isClosedForRead)) return fromExtension
val contentPacket = content.readRemaining()

for (ext in extensions) {
if (contentPacket.exhausted()) return null
return ext.deserialize(charset, typeInfo, ByteReadChannel(contentPacket)) ?: continue
}
Comment thread
Stexxe marked this conversation as resolved.

val serializer = format.serializersModule.serializerForTypeInfo(typeInfo)
val contentPacket = content.readRemaining()

try {
return when (format) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.serialization.kotlinx

import io.ktor.serialization.kotlinx.json.DefaultJson
import io.ktor.util.reflect.typeInfo
import io.ktor.utils.io.ByteChannel
import io.ktor.utils.io.charsets.Charsets
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertNull
import kotlin.time.Duration.Companion.milliseconds

@Serializable
data class Payload(val value: String)

class ConverterTest {
@Test
fun returnsNullForEmptyChannelWithDelayedClose() = runTest {
val channel = ByteChannel()
launch {
delay(200.milliseconds)
channel.close()
}

val converter = KotlinxSerializationConverter(DefaultJson)
val result = converter.deserialize(Charsets.UTF_8, typeInfo<Payload>(), channel)
assertNull(result)
}
}