Skip to content

Spring using kotlin serialization over jackson for its own classes #26321

@tomfi

Description

@tomfi

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: 43faa43

To be more precise the order of message converters changed between 5.3.1 and 5.3.2:

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?

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issuetype: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions