Skip to content

Commit

Permalink
PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
wkornewald committed Nov 11, 2024
1 parent a3d2436 commit 3ba2335
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ktor-http/api/ktor-http.api
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ public final class io/ktor/http/UrlKt {
}

public final class io/ktor/http/UrlSerializer : kotlinx/serialization/KSerializer {
public fun <init> ()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;
Expand Down
18 changes: 8 additions & 10 deletions ktor-http/api/ktor-http.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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/Url> { // io.ktor.http/UrlSerializer|null[0]
constructor <init>() // io.ktor.http/UrlSerializer.<init>|<init>(){}[0]

final val descriptor // io.ktor.http/UrlSerializer.descriptor|{}descriptor[0]
final fun <get-descriptor>(): kotlinx.serialization.descriptors/SerialDescriptor // io.ktor.http/UrlSerializer.descriptor.<get-descriptor>|<get-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 <get-authScheme>(): kotlin/String // io.ktor.http.auth/HttpAuthHeader.authScheme.<get-authScheme>|<get-authScheme>(){}[0]
Expand Down Expand Up @@ -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/Url> { // io.ktor.http/UrlSerializer|null[0]
final val descriptor // io.ktor.http/UrlSerializer.descriptor|{}descriptor[0]
final fun <get-descriptor>(): kotlinx.serialization.descriptors/SerialDescriptor // io.ktor.http/UrlSerializer.descriptor.<get-descriptor>|<get-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 <get-DEFAULT_PORT>(): kotlin/Int // io.ktor.http/DEFAULT_PORT.<get-DEFAULT_PORT>|<get-DEFAULT_PORT>(){}[0]

Expand Down
2 changes: 2 additions & 0 deletions ktor-http/common/src/io/ktor/http/Cookie.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down
1 change: 1 addition & 0 deletions ktor-http/common/src/io/ktor/http/URLProtocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
7 changes: 5 additions & 2 deletions ktor-http/common/src/io/ktor/http/Url.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -263,8 +265,9 @@ internal val Url.encodedUserAndPassword: String
appendUserAndPassword(encodedUser, encodedPassword)
}

public class UrlSerializer : KSerializer<Url> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Url", PrimitiveKind.STRING)
public object UrlSerializer : KSerializer<Url> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("io.ktor.http.Url", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): Url =
Url(decoder.decodeString())
Expand Down
6 changes: 2 additions & 4 deletions ktor-http/jvm/test/io/ktor/tests/http/SerializableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
4 changes: 4 additions & 0 deletions ktor-io/common/src/io/ktor/utils/io/JvmSerializable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : JvmSerializable {
public fun jvmSerialize(value: T): ByteArray
public fun jvmDeserialize(value: ByteArray): T
}

@InternalAPI
public expect fun <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any

internal object DummyJvmSimpleSerializerReplacement
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any =
DummyJvmSimpleSerializerReplacement
3 changes: 3 additions & 0 deletions ktor-io/jvm/src/io/ktor/utils/io/JvmSerializable.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any =
DefaultJvmSerializerReplacement(serializer, value)

@OptIn(InternalAPI::class)
@PublishedApi // IMPORTANT: changing the class name would result in serialization incompatibility
internal class DefaultJvmSerializerReplacement<T : Any>(
private var serializer: JvmSerializer<T>?,
Expand Down
3 changes: 2 additions & 1 deletion ktor-io/posix/src/io/ktor/utils/io/JvmSerializable.posix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> JvmSerializerReplacement(serializer: JvmSerializer<T>, value: T): Any =
DummyJvmSimpleSerializerReplacement
9 changes: 7 additions & 2 deletions ktor-shared/ktor-junit/jvm/src/io/ktor/junit/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.ktor.junit

import java.io.*
import kotlin.test.*

/**
* Convenience function for asserting on all elements of a collection.
Expand Down Expand Up @@ -32,9 +33,13 @@ fun <T> assertAll(collection: Iterable<T>, assertion: (T) -> Unit) {
)
}

inline fun <reified T : Any> assertSerializable(obj: T): T {
inline fun <reified T : Any> 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
}

0 comments on commit 3ba2335

Please sign in to comment.