Skip to content

Commit fa8f38c

Browse files
sandwwraithSpace Team
authored and
Space Team
committed
Don't fail if there is no serializer for type parameters of contextual serializer
Return null instead. Such behaviour is needed to support cachedChildSerializers logic. Since this field creator doesn't provide genericGetter (because it's static), type param serializer can't be retrieved, and the whole contextual serializer shouldn't be cached. #KT-58067 Fixed
1 parent bf757d3 commit fa8f38c

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/BaseIrGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ abstract class BaseIrGenerator(private val currentClass: IrClass, final override
519519
compilerContext,
520520
it
521521
)
522-
instantiate(argSer, it)!!
522+
instantiate(argSer, it) ?: return null
523523
})
524524
)
525525
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// TARGET_BACKEND: JVM_IR
2+
3+
// WITH_STDLIB
4+
5+
import kotlinx.serialization.*
6+
import kotlinx.serialization.json.*
7+
import kotlinx.serialization.descriptors.*
8+
import kotlinx.serialization.modules.*
9+
import kotlinx.serialization.encoding.*
10+
11+
class SomeData<T>(val t: T)
12+
13+
@Serializable
14+
class PagedData<T>(
15+
@Contextual val someData: SomeData<T>,
16+
)
17+
18+
class SomeDataSerializer<T>(val tSer: KSerializer<T>) : KSerializer<SomeData<T>> {
19+
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("SomeData")
20+
21+
override fun serialize(encoder: Encoder, value: SomeData<T>) {
22+
encoder as JsonEncoder
23+
val data = encoder.json.encodeToJsonElement(tSer, value.t)
24+
val obj = buildJsonObject {
25+
put("innerType", tSer.descriptor.serialName)
26+
put("data", data)
27+
}
28+
encoder.encodeJsonElement(obj)
29+
}
30+
31+
override fun deserialize(decoder: Decoder): SomeData<T> {
32+
TODO("Not yet implemented")
33+
}
34+
}
35+
36+
fun box(): String {
37+
val module = SerializersModule {
38+
contextual(SomeData::class) { args -> SomeDataSerializer(args[0]) }
39+
}
40+
val json = Json { serializersModule = module }
41+
val input = PagedData<String>(SomeData("foo_bar"))
42+
val enc = json.encodeToString(input)
43+
return if (enc != """{"someData":{"innerType":"kotlin.String","data":"foo_bar"}}""") enc else "OK"
44+
}

plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)