Skip to content

Kotlin Serialization can't be used for Actuator #47176

@mhalbritter

Description

@mhalbritter

While Kotlin Serialization can be used for controllers, it doesn't work to serialize DTOs returned from Actuator @Endpoints.

When setting spring.http.converters.preferred-json-mapper=gson (or jsonb), Gson is used for controllers and actuator endpoints.

When setting spring.http.converters.preferred-json-mapper=kotlin-serialization, Kotlin Serialization is used for controllers, but Jackson is used for actuator endpoints.

This demo project (sorry that it's in Kotlin, but Kotlin Serialization only works with Kotlin types) demonstrates the issue.

I did a bit of debugging: A breakpoint in org.springframework.http.converter.AbstractKotlinSerializationHttpMessageConverter#canWrite reveals:

In the WebMVC case:

  • type is com.example.kotlin_serialization.MyDto
  • clazz is class com.example.kotlin_serialization.MyDto

In the actuator case:

  • type is java.lang.Object
  • clazz is class com.example.kotlin_serialization.MyDto

And Spring Framework uses type to see if there's a generated serializer for the type (the serializers are generated by a compiler plugin, see build.gradle).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions