kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Classes loaded to diffrent class-loaders have equal serial descriptor

Open kunyavskiy opened this issue 1 month ago • 4 comments

Describe the bug

If one loads the same serializable class in two different class-loaders, corresponding serial descriptors are equal (in sense desctiptor1 == descritor2 returns true, while they are different.

Attach a code snippet or test data if possible.

test-kotlinx-serialization-with-classloaders.zip

Expected behavior

Different descriptors of different serializers are not equal.

kunyavskiy avatar Nov 12 '25 17:11 kunyavskiy

Actully, I'm not sure if it is a problem with classloaders, or with any two serial descriptors with same serial name. But that looks like a reasonable way of having such a clash.

kunyavskiy avatar Nov 12 '25 17:11 kunyavskiy

Actully, I'm not sure if it is a problem with classloaders, or with any two serial descriptors with same serial name. But that looks like a reasonable way of having such a clash.

Yep, serial names have to be unique: https://github.com/Kotlin/kotlinx.serialization/pull/3098

fzhinkin avatar Nov 12 '25 17:11 fzhinkin

The question is whether it is reasonable to handle this case in the equals of a descriptor (for the few cases this matters). The biggest actual issue would be a case where two different implementations are loaded, and then attempted to be serialized by the same format instance (where the instance caches some of its operations). Ensuring unique names would not really be feasible in this context though.

pdvrieze avatar Nov 12 '25 21:11 pdvrieze

I'm not sure we can deliver a meaningful fix here. Contract for SerialDescriptor equality is fixed and will likely be too hard to change at this points. Besides, there' really nothing else we can compare in equals besides maybe this::class.classLoader

sandwwraith avatar Nov 24 '25 13:11 sandwwraith