Closed fkneier-bikeleasing closed 4 months ago
This really is a major problem since it can not be disabled easily. But for now I found this hacky workaround:
@Bean
fun disableKotlinSerializationJsonCustomizer() = CodecCustomizer {
if (ClassUtils.isPresent("kotlinx.serialization.json.Json", javaClass.classLoader)) {
it.defaultCodecs().kotlinSerializationJsonDecoder(
object : KotlinSerializationJsonDecoder() {
override fun canDecode(elementType: ResolvableType, mimeType: MimeType?): Boolean = false
}
)
it.defaultCodecs().kotlinSerializationJsonEncoder(
object : KotlinSerializationJsonEncoder() {
override fun canEncode(elementType: ResolvableType, mimeType: MimeType?): Boolean = false
}
)
}
}
Kotlin Serialization does not work exactly like Jackson, so I suspect what you are looking for is to disable the default codec registration which on purpose uses Kotlin Serialization when it is on the classpath, and just enable the ones you want like Jackson and potentially a few others. If you need more help, Stack Overflow is probably a better place to ask, since we would like to keep the bug tracker focused on Spring Framework issues.
As a consequence, I close this issue as invalid. If you think otherwise, please describe the Spring Framework issue in detail. Just please take in account that automatic registration of Kotlin Serialization which takes precedence over Jackson when present on the classpath is the expected behavior and is not a bug.
In certain cases Kotlin Serialization does not work at all. It is broken, which is clearly stated in the exception. I'm actually stunned that this ticket has been closed as invalid. This is not a matter of preference.
Either the type resolution has to be fixed (which DOES NOT correctly resolve nullability) or Kotlin Serialization must not be used in these cases (which DOES check nullability).
But currently a controller method with a valid body parameter has become unusable when Kotlin Serialization is used instead of Jackson, which is the default handling if Kotlin Serialization is found in the classpath.
Kotlin Serialization sometimes by design works differently (for example you have to annotate the classes with @Serializable
) so I thought initially that was just another difference, but after another look it looks like indeed that we are indeed losing the null-safety information probably before ResolvableType
don't have this information. A related hint could be pass via AbstractMessageReaderArgumentResolver#readBody(MethodParameter, MethodParameter, boolean, BindingContext, ServerWebExchange)
but it is not obvious how to do that in a non intrusive way. I need to discuss that with the team.
ResolvableType#getSource
should allow to get the KType
with the relevant null-safety information on WebFlux. For WebMVC, that's less obvious.
Isn't this considered a bug? Maybe even a serious one? I think it breaks existing code just because of the existence of a dependency and there even isn't an easy workaround.
If you do not want to use Kotlin Serialization, you can easily bring back Jackson as explain above.
Depends on #33118.
The json deserialization is broken if
kotlinx-serialization-json
is in classpath. It seems that type nullability information is lost when using KotlinSerializationStringDecoder.