Skip to content

Commit b26c67a

Browse files
authored
Tests for fixes for #KT-62522 and #KT-62215 (#2474)
1 parent 8853917 commit b26c67a

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.serialization
6+
7+
import kotlinx.serialization.builtins.serializer
8+
import kotlinx.serialization.json.*
9+
import kotlin.test.Test
10+
11+
class GenericOverrideTest: JsonTestBase() {
12+
13+
@Serializable
14+
sealed class TypedSealedClass<T>(val a: T) {
15+
@Serializable
16+
@SerialName("child")
17+
data class Child(val y: Int) : TypedSealedClass<String>("10") {
18+
override fun toString(): String = "Child($a, $y)"
19+
}
20+
}
21+
22+
@Test
23+
fun testAinChildSerializesAsString() = parametrizedTest { mode ->
24+
val encodedChild = """{"a":"10","y":42}"""
25+
assertJsonFormAndRestored(TypedSealedClass.Child.serializer(), TypedSealedClass.Child(42), encodedChild)
26+
}
27+
28+
@Test
29+
fun testSerializeAsBaseClass() = parametrizedTest { mode ->
30+
val encodedChild = """{"type":"child","a":"10","y":42}"""
31+
assertJsonFormAndRestored(TypedSealedClass.serializer(String.serializer()), TypedSealedClass.Child(42), encodedChild)
32+
}
33+
}

integration-test/src/commonMain/kotlin/sample/MultiFileHierarchyModuleA.kt

+12
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ abstract class NotInConstructorBase {
2828
val b = "val b"
2929
val a = "val a"
3030
}
31+
32+
@Serializable
33+
open class GenericBox<E> {
34+
var contents: Map<String, E>? = null
35+
}
36+
37+
// From #KT-43910
38+
@Serializable
39+
open class ValidatableValue<T : Any, V: Any>(
40+
var value: T? = null,
41+
var error: V? = null,
42+
)

integration-test/src/commonTest/kotlin/sample/MultiFileHierarchyModuleB.kt

+14
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ class NotInConstructorTest : NotInConstructorBase() {
6060
return a.hashCode() * 31 + b.hashCode() * 31 + c.hashCode()
6161
}
6262
}
63+
64+
@Serializable
65+
data class TestData(val field: String)
66+
67+
68+
@Serializable
69+
class TestClass(): GenericBox<TestData>()
70+
71+
@Serializable
72+
class Email<E : Any> : ValidatableValue<String, E>() {
73+
override fun toString(): String {
74+
return "Email($value, $error)"
75+
}
76+
}

integration-test/src/commonTest/kotlin/sample/MultiFileHierarchyTest.kt

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package sample
66

7+
import kotlinx.serialization.encodeToString
78
import kotlinx.serialization.json.Json
89
import kotlinx.serialization.modules.SerializersModule
910
import kotlinx.serialization.modules.polymorphic
@@ -73,4 +74,23 @@ class AbstractBaseTest {
7374
fun testPropertiesNotInConstructor() {
7475
assertStringFormAndRestored("""{"b":"val b","a":"val a","c":"val c"}""", NotInConstructorTest(), NotInConstructorTest.serializer())
7576
}
77+
78+
@Test
79+
fun testDoubleGeneric() {
80+
val email = Email<Int>().apply {
81+
value = "foo"
82+
error = 1
83+
}
84+
val encodedEmail = Json.encodeToString(email)
85+
assertEquals("""{"value":"foo","error":1}""", encodedEmail)
86+
assertEquals("Email(foo, 1)", Json.decodeFromString<Email<Int>>(encodedEmail).toString())
87+
}
88+
89+
@Test
90+
fun test() {
91+
val t = TestClass().also { it.contents = mapOf("a" to TestData("data")) }
92+
val s = Json.encodeToString(t)
93+
assertEquals("""{"contents":{"a":{"field":"data"}}}""", s)
94+
assertEquals("data", Json.decodeFromString<TestClass>(s).contents?.get("a")?.field)
95+
}
7696
}

0 commit comments

Comments
 (0)