Skip to content

Commit d5e97c6

Browse files
sandwwraithSpace Team
authored and
Space Team
committed
Skip properties from Java classes for which the getter type is unknown
thus avoiding NPE when auto-generating external serializers. #KT-55681 Fixed (cherry picked from commit 0b4f534)
1 parent c8a4ba1 commit d5e97c6

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
44
*/
55

@@ -74,7 +74,7 @@ internal fun serializablePropertiesForIrBackend(
7474
if (irClass.isInternalSerializable) !it.annotations.hasAnnotation(SerializationAnnotations.serialTransientFqName)
7575
else !DescriptorVisibilities.isPrivate(it.visibility) && ((it.isVar && !it.annotations.hasAnnotation(SerializationAnnotations.serialTransientFqName)) || primaryParamsAsProps.contains(
7676
it
77-
))
77+
)) && it.getter?.returnType != null // For some reason, some properties from Java (like java.net.URL.hostAddress) do not have getter. Let's ignore them, as they never have worked properly in K1 either.
7878

7979
val (primaryCtorSerializableProps, bodySerializableProps) = properties
8080
.asSequence()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// IGNORE_BACKEND_FIR: JVM_IR
2+
// TARGET_BACKEND: JVM_IR
3+
4+
// WITH_STDLIB
5+
6+
import kotlinx.serialization.*
7+
import kotlinx.serialization.descriptors.*
8+
import kotlinx.serialization.encoding.*
9+
import java.net.URL
10+
11+
12+
// @Serializer should do nothing if all methods are overriden
13+
@Serializer(forClass = URL::class)
14+
object URLSerializer : KSerializer<URL> {
15+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("java.net.URL", PrimitiveKind.STRING)
16+
17+
override fun serialize(encoder: Encoder, value: URL) {
18+
TODO()
19+
}
20+
21+
override fun deserialize(decoder: Decoder): URL {
22+
TODO()
23+
}
24+
}
25+
26+
fun box(): String {
27+
if (URLSerializer.descriptor.toString() != "PrimitiveDescriptor(java.net.URL)") return URLSerializer.descriptor.toString()
28+
return "OK"
29+
}

plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirBlackBoxTestGenerated.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)