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

[Kotlin 2.1.20] Tests for fixes for #KT-62522 and #KT-62215 #2474

Open
wants to merge 2 commits into
base: dev
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
@@ -0,0 +1,33 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization

import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.*
import kotlin.test.Test

class GenericOverrideTest: JsonTestBase() {

@Serializable
sealed class TypedSealedClass<T>(val a: T) {
@Serializable
@SerialName("child")
data class Child(val y: Int) : TypedSealedClass<String>("10") {
override fun toString(): String = "Child($a, $y)"
}
}

@Test
fun testAinChildSerializesAsString() = parametrizedTest { mode ->
val encodedChild = """{"a":"10","y":42}"""
assertJsonFormAndRestored(TypedSealedClass.Child.serializer(), TypedSealedClass.Child(42), encodedChild)
}

@Test
fun testSerializeAsBaseClass() = parametrizedTest { mode ->
val encodedChild = """{"type":"child","a":"10","y":42}"""
assertJsonFormAndRestored(TypedSealedClass.serializer(String.serializer()), TypedSealedClass.Child(42), encodedChild)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ abstract class NotInConstructorBase {
val b = "val b"
val a = "val a"
}

@Serializable
open class GenericBox<E> {
var contents: Map<String, E>? = null
}

// From #KT-43910
@Serializable
open class ValidatableValue<T : Any, V: Any>(
var value: T? = null,
var error: V? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ class NotInConstructorTest : NotInConstructorBase() {
return a.hashCode() * 31 + b.hashCode() * 31 + c.hashCode()
}
}

@Serializable
data class TestData(val field: String)


@Serializable
class TestClass(): GenericBox<TestData>()

@Serializable
class Email<E : Any> : ValidatableValue<String, E>() {
override fun toString(): String {
return "Email($value, $error)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package sample

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
Expand Down Expand Up @@ -73,4 +74,23 @@ class AbstractBaseTest {
fun testPropertiesNotInConstructor() {
assertStringFormAndRestored("""{"b":"val b","a":"val a","c":"val c"}""", NotInConstructorTest(), NotInConstructorTest.serializer())
}

@Test
fun testDoubleGeneric() {
val email = Email<Int>().apply {
value = "foo"
error = 1
}
val encodedEmail = Json.encodeToString(email)
assertEquals("""{"value":"foo","error":1}""", encodedEmail)
assertEquals("Email(foo, 1)", Json.decodeFromString<Email<Int>>(encodedEmail).toString())
}

@Test
fun test() {
val t = TestClass().also { it.contents = mapOf("a" to TestData("data")) }
val s = Json.encodeToString(t)
assertEquals("""{"contents":{"a":{"field":"data"}}}""", s)
assertEquals("data", Json.decodeFromString<TestClass>(s).contents?.get("a")?.field)
}
}