Skip to content

Commit 0b4f534

Browse files
sandwwraithSpace Team
authored andcommitted
Skip properties from Java classes for which the getter type is unknown
thus avoiding NPE when auto-generating external serializers. #KT-55681 Fixed
1 parent a1894ed commit 0b4f534

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

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

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

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)