Configure Kotlin JSON converters as Jackson alternative

Prior to this commit, Spring MVC and WebFlux would consider the
"kotlinx.serialization" JSON codecs and converters in addition to other
JSON alternatives like Jackson, Gson and Jsonb.

This would cause issues because while in most cases this library is only
involved if the type is annotated with "@Serializable", this is not true
for Java enums. In this particular case, the codec shadows Jackson and
causes issues.

This commit now considers kotlinx.serialization JSON support as an
alternative to Jackson. Just like Jsonb and GSON, this is only
auto-detected if Jackson is not present.
We received consistent feedback that kotlinx.serialization is popular in
Kotlin libraries and is often a transitive dependency. As a result, we
cannot consider its presence on the classpath as a strong enough signal
to configure it by default.

Closes gh-34410
This commit is contained in:
Brian Clozel
2025-02-11 20:05:53 +01:00
parent 6ded25ba28
commit 7077809561
8 changed files with 30 additions and 41 deletions

View File

@@ -894,9 +894,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
if (kotlinSerializationCborPresent) {
messageConverters.add(new KotlinSerializationCborHttpMessageConverter());
}
if (kotlinSerializationJsonPresent) {
messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}
if (kotlinSerializationProtobufPresent) {
messageConverters.add(new KotlinSerializationProtobufHttpMessageConverter());
}
@@ -914,6 +911,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
else if (jsonbPresent) {
messageConverters.add(new JsonbHttpMessageConverter());
}
else if (kotlinSerializationJsonPresent) {
messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}
if (jackson2SmilePresent) {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();