Affects: 5.3.2

In my project I am using kotlin serialization internally for kotlin data classes but expected spring/spring-boot to use jackson as HTTP message converter which was fine until i tried to upgrade from spring boot 2.4.0 to 2.4.1.

After the upgrade, my unit tests failed with the following exception;

2020-12-28 09:15:12.624 ERROR 4263 --- [           main] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: HttpMessageNotWritableException, Could not write JSON: Class 'DefaultOAuth2AccessToken' is not registered for polymorphic serialization in the scope of 'OAuth2AccessToken'.
Mark the base class as 'sealed' or register the serializer explicitly.; nested exception is kotlinx.serialization.SerializationException: Class 'DefaultOAuth2AccessToken' is not registered for polymorphic serialization in the scope of 'OAuth2AccessToken'.
Mark the base class as 'sealed' or register the serializer explicitly.

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Class 'DefaultOAuth2AccessToken' is not registered for polymorphic serialization in the scope of 'OAuth2AccessToken'.
Mark the base class as 'sealed' or register the serializer explicitly.; nested exception is kotlinx.serialization.SerializationException: Class 'DefaultOAuth2AccessToken' is not registered for polymorphic serialization in the scope of 'OAuth2AccessToken'.
Mark the base class as 'sealed' or register the serializer explicitly.
    at org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter.encode(KotlinSerializationJsonHttpMessageConverter.java:161)
    at org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter.writeInternal(KotlinSerializationJsonHttpMessageConverter.java:145)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)

Looking further and debugging i found the following change: https://github.com/spring-projects/spring-framework/commit/43faa439ab567c382e50ae670560024cebdf63d2

To be more precise the order of message converters changed between 5.3.1 and 5.3.2: * In 5.3.1 jackson is registered before kotlin serialization -> https://github.com/spring-projects/spring-framework/blob/v5.3.1/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java#L921 * In 5.3.2 jackson is registered after kotlin serialization -> https://github.com/spring-projects/spring-framework/blob/v5.3.2/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java#L909

I tried to find a way to change the order in a simple way or disable kotlin serialization for spring without success. I would like to avoid registering the message converters manually if possible.

Is there a way to workaroud the issue? Is it intentional that kotlin serialization will be used for classes that are not marked as @Serializable and further then that classes that are internal to spring?

Comment From: sdeleuze

Likely a duplicate of #26298.