diff --git a/ktor-http/api/ktor-http.api b/ktor-http/api/ktor-http.api index 3b3da432dff..3f490eb1bf2 100644 --- a/ktor-http/api/ktor-http.api +++ b/ktor-http/api/ktor-http.api @@ -1062,7 +1062,7 @@ public final class io/ktor/http/UrlKt { } public final class io/ktor/http/UrlSerializer : kotlinx/serialization/KSerializer { - public fun ()V + public static final field INSTANCE Lio/ktor/http/UrlSerializer; public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lio/ktor/http/Url; public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object; public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor; diff --git a/ktor-http/api/ktor-http.klib.api b/ktor-http/api/ktor-http.klib.api index 7ab441b73c7..c4372c4f9a4 100644 --- a/ktor-http/api/ktor-http.klib.api +++ b/ktor-http/api/ktor-http.klib.api @@ -1134,16 +1134,6 @@ final class io.ktor.http/Url : io.ktor.utils.io/JvmSerializable { // io.ktor.htt } } -final class io.ktor.http/UrlSerializer : kotlinx.serialization/KSerializer { // io.ktor.http/UrlSerializer|null[0] - constructor () // io.ktor.http/UrlSerializer.|(){}[0] - - final val descriptor // io.ktor.http/UrlSerializer.descriptor|{}descriptor[0] - final fun (): kotlinx.serialization.descriptors/SerialDescriptor // io.ktor.http/UrlSerializer.descriptor.|(){}[0] - - final fun deserialize(kotlinx.serialization.encoding/Decoder): io.ktor.http/Url // io.ktor.http/UrlSerializer.deserialize|deserialize(kotlinx.serialization.encoding.Decoder){}[0] - final fun serialize(kotlinx.serialization.encoding/Encoder, io.ktor.http/Url) // io.ktor.http/UrlSerializer.serialize|serialize(kotlinx.serialization.encoding.Encoder;io.ktor.http.Url){}[0] -} - sealed class io.ktor.http.auth/HttpAuthHeader { // io.ktor.http.auth/HttpAuthHeader|null[0] final val authScheme // io.ktor.http.auth/HttpAuthHeader.authScheme|{}authScheme[0] final fun (): kotlin/String // io.ktor.http.auth/HttpAuthHeader.authScheme.|(){}[0] @@ -1592,6 +1582,14 @@ final object io.ktor.http/HttpHeaders { // io.ktor.http/HttpHeaders|null[0] final fun isUnsafe(kotlin/String): kotlin/Boolean // io.ktor.http/HttpHeaders.isUnsafe|isUnsafe(kotlin.String){}[0] } +final object io.ktor.http/UrlSerializer : kotlinx.serialization/KSerializer { // io.ktor.http/UrlSerializer|null[0] + final val descriptor // io.ktor.http/UrlSerializer.descriptor|{}descriptor[0] + final fun (): kotlinx.serialization.descriptors/SerialDescriptor // io.ktor.http/UrlSerializer.descriptor.|(){}[0] + + final fun deserialize(kotlinx.serialization.encoding/Decoder): io.ktor.http/Url // io.ktor.http/UrlSerializer.deserialize|deserialize(kotlinx.serialization.encoding.Decoder){}[0] + final fun serialize(kotlinx.serialization.encoding/Encoder, io.ktor.http/Url) // io.ktor.http/UrlSerializer.serialize|serialize(kotlinx.serialization.encoding.Encoder;io.ktor.http.Url){}[0] +} + final const val io.ktor.http/DEFAULT_PORT // io.ktor.http/DEFAULT_PORT|{}DEFAULT_PORT[0] final fun (): kotlin/Int // io.ktor.http/DEFAULT_PORT.|(){}[0] diff --git a/ktor-http/common/src/io/ktor/http/Cookie.kt b/ktor-http/common/src/io/ktor/http/Cookie.kt index fcf2b0ee11b..6872c1d466a 100644 --- a/ktor-http/common/src/io/ktor/http/Cookie.kt +++ b/ktor-http/common/src/io/ktor/http/Cookie.kt @@ -2,6 +2,8 @@ * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ +@file:OptIn(InternalAPI::class) + package io.ktor.http import io.ktor.util.* diff --git a/ktor-http/common/src/io/ktor/http/URLProtocol.kt b/ktor-http/common/src/io/ktor/http/URLProtocol.kt index 513e6287014..b6f9fc9e141 100644 --- a/ktor-http/common/src/io/ktor/http/URLProtocol.kt +++ b/ktor-http/common/src/io/ktor/http/URLProtocol.kt @@ -12,6 +12,7 @@ import io.ktor.utils.io.* * @property name of protocol (schema) * @property defaultPort default port for protocol or `-1` if not known */ +@OptIn(InternalAPI::class) public data class URLProtocol(val name: String, val defaultPort: Int) : JvmSerializable { init { require(name.all { it.isLowerCase() }) { "All characters should be lower case" } diff --git a/ktor-http/common/src/io/ktor/http/Url.kt b/ktor-http/common/src/io/ktor/http/Url.kt index 60c94e0aeec..8f492789b28 100644 --- a/ktor-http/common/src/io/ktor/http/Url.kt +++ b/ktor-http/common/src/io/ktor/http/Url.kt @@ -2,6 +2,8 @@ * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ +@file:OptIn(InternalAPI::class) + package io.ktor.http import io.ktor.utils.io.* @@ -263,8 +265,9 @@ internal val Url.encodedUserAndPassword: String appendUserAndPassword(encodedUser, encodedPassword) } -public class UrlSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Url", PrimitiveKind.STRING) +public object UrlSerializer : KSerializer { + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("io.ktor.http.Url", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): Url = Url(decoder.decodeString()) diff --git a/ktor-http/jvm/test/io/ktor/tests/http/SerializableTest.kt b/ktor-http/jvm/test/io/ktor/tests/http/SerializableTest.kt index bc2e21fc599..e3df54915e2 100644 --- a/ktor-http/jvm/test/io/ktor/tests/http/SerializableTest.kt +++ b/ktor-http/jvm/test/io/ktor/tests/http/SerializableTest.kt @@ -12,13 +12,11 @@ import kotlin.test.* class SerializableTest { @Test fun urlTest() { - val url = Url("https://localhost/path?key=value#fragment") - assertEquals(url, assertSerializable(url)) + assertSerializable(Url("https://localhost/path?key=value#fragment")) } @Test fun cookieTest() { - val cookie = Cookie("key", "value") - assertEquals(cookie, assertSerializable(cookie)) + assertSerializable(Cookie("key", "value")) } } diff --git a/ktor-io/common/src/io/ktor/utils/io/JvmSerializable.kt b/ktor-io/common/src/io/ktor/utils/io/JvmSerializable.kt index 490fdef56c8..ae6f583e692 100644 --- a/ktor-io/common/src/io/ktor/utils/io/JvmSerializable.kt +++ b/ktor-io/common/src/io/ktor/utils/io/JvmSerializable.kt @@ -4,13 +4,17 @@ package io.ktor.utils.io +/** Alias for `java.io.Serializable` on JVM. Empty interface otherwise. */ +@InternalAPI public expect interface JvmSerializable +@InternalAPI public interface JvmSerializer : JvmSerializable { public fun jvmSerialize(value: T): ByteArray public fun jvmDeserialize(value: ByteArray): T } +@InternalAPI public expect fun JvmSerializerReplacement(serializer: JvmSerializer, value: T): Any internal object DummyJvmSimpleSerializerReplacement diff --git a/ktor-io/jsAndWasmShared/src/io/ktor/utils/io/JvmSerializable.jsAndWasmShared.kt b/ktor-io/jsAndWasmShared/src/io/ktor/utils/io/JvmSerializable.jsAndWasmShared.kt index 5fcedcdd9b3..44454f494a0 100644 --- a/ktor-io/jsAndWasmShared/src/io/ktor/utils/io/JvmSerializable.jsAndWasmShared.kt +++ b/ktor-io/jsAndWasmShared/src/io/ktor/utils/io/JvmSerializable.jsAndWasmShared.kt @@ -4,8 +4,9 @@ package io.ktor.utils.io -/** Alias for `java.io.Serializable` on JVM. Empty interface otherwise. */ +@InternalAPI public actual interface JvmSerializable +@InternalAPI public actual fun JvmSerializerReplacement(serializer: JvmSerializer, value: T): Any = DummyJvmSimpleSerializerReplacement diff --git a/ktor-io/jvm/src/io/ktor/utils/io/JvmSerializable.jvm.kt b/ktor-io/jvm/src/io/ktor/utils/io/JvmSerializable.jvm.kt index 875bf5b8edb..608cc65229a 100644 --- a/ktor-io/jvm/src/io/ktor/utils/io/JvmSerializable.jvm.kt +++ b/ktor-io/jvm/src/io/ktor/utils/io/JvmSerializable.jvm.kt @@ -6,12 +6,15 @@ package io.ktor.utils.io import java.io.* +@InternalAPI public actual typealias JvmSerializable = Serializable @Suppress("UNCHECKED_CAST") +@InternalAPI public actual fun JvmSerializerReplacement(serializer: JvmSerializer, value: T): Any = DefaultJvmSerializerReplacement(serializer, value) +@OptIn(InternalAPI::class) @PublishedApi // IMPORTANT: changing the class name would result in serialization incompatibility internal class DefaultJvmSerializerReplacement( private var serializer: JvmSerializer?, diff --git a/ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.kt b/ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.kt index 5fcedcdd9b3..44454f494a0 100644 --- a/ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.kt +++ b/ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.kt @@ -4,8 +4,9 @@ package io.ktor.utils.io -/** Alias for `java.io.Serializable` on JVM. Empty interface otherwise. */ +@InternalAPI public actual interface JvmSerializable +@InternalAPI public actual fun JvmSerializerReplacement(serializer: JvmSerializer, value: T): Any = DummyJvmSimpleSerializerReplacement diff --git a/ktor-shared/ktor-junit/jvm/src/io/ktor/junit/Assertions.kt b/ktor-shared/ktor-junit/jvm/src/io/ktor/junit/Assertions.kt index f3857c1e453..9e1545f0fb4 100644 --- a/ktor-shared/ktor-junit/jvm/src/io/ktor/junit/Assertions.kt +++ b/ktor-shared/ktor-junit/jvm/src/io/ktor/junit/Assertions.kt @@ -5,6 +5,7 @@ package io.ktor.junit import java.io.* +import kotlin.test.* /** * Convenience function for asserting on all elements of a collection. @@ -32,9 +33,13 @@ fun assertAll(collection: Iterable, assertion: (T) -> Unit) { ) } -inline fun assertSerializable(obj: T): T { +inline fun assertSerializable(obj: T, checkEquality: Boolean = true): T { val encoded = ByteArrayOutputStream().also { ObjectOutputStream(it).writeObject(obj) }.toByteArray() - return ObjectInputStream(encoded.inputStream()).readObject() as T + val decoded = ObjectInputStream(encoded.inputStream()).readObject() as T + if (checkEquality) { + assertEquals(obj, decoded, "deserialized object must be equal to original object") + } + return decoded }