You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add first class support for kotlinx.serialization #24238
using Spring Boot 2.4.0)
spring-boot-starter-actuator has strong dependencies on jackson so that I can't use kotlinx.serialization as default json message converter.
Reacted by Subhod I, Alberto Cavalcante, Sumanth Chinthagunta, André Thiele, Rodolpho Couto, Piotr Krzemiński, Junsu Jang, Patrice Manser, Aseem Savio, Christopher Orr and 8 more
Hi, thanks for raising this since indeed I think there is a need for a proper integration for Kotlin serialization in Boot that goes further than what is inherited from Spring Framework.
Current behavior is documented here and has several limitations in Boot context that we would be likely to improve.
The configuration is kind of Spring Framework oriented, a dedicated Boot support similar to JSON-B and related documentation would be much better for consistency.
The second point that should be improved is what you raised in the issue. After discussing with @bclozel I tend to think it is currently not straightforward to support Kotlin serialization and actuators because the web configuration is shared between your app (that wants to use Kotlin serialization) and and actuator (that needs Jackson or similar general purpose JSON library to be configured in your web configuration. And Kotlin serialization is designed to serialize only Kotlin classes annotated with @Serializable or collection of those.
On Framework side, maybe I could refine the converter selection mechanism to have a better support for having both Kotlin Serialization and Jackson via spring-projects/spring-framework#26147 in order to provide a workaround with manual config of both.
On Boot side, after discussing with @snicoll and @bclozel , it sounds like a candidate for Spring Boot 2.5 that would potentially involve the resolution of #20291. As well as dependency management (I am discussing the creation of a Kotlin serialization BOM with the Kotlin team).
Reacted by Alberto Cavalcante, Sumanth Chinthagunta and beltram
@snicoll After working on a draft commit for spring-projects/spring-framework#26147, I confirm that I plan to configure both Kotlin serialization and Jackson when they are on the classpath, I think that's will make configuration easier and will make actuator and error endpoints still working. Kotlin serialization is more narrow so that seems to work as expected.
So for Boot 2.5, what would be mainly needed is autoconfiguration and documentation à la JSONB, and dependency management with the upcoming BOM that I have asked to Kotlin team (similar to Coroutines one).
changed the title [-]Actuator prevents use of KotlinSerializationJsonHttpMessageConverter as default json message converter[/-][+]Add auto-configuration for kotlinx.serialization[/+]on Nov 26, 2020
@neostage I have fixed spring-projects/spring-framework#26147 and made sure your use case work fine with Boot 2.4, please test Spring Framework 5.3.2-SNAPSHOT with both Jackson and Kotlin serialization in the classpath it should work as expected.
We also have a similar issue where we have the dependency on kotest-assertions-json in testImplementation (which brings in kotlinx.serialization as transitive dependency).
This causes an error in a MockMVC test, where an Endpoint returning ResponseEntity<Unit> is called, if we return a ProblemDetail (or any response with a body in case of a failure), as kotlinx.serialization.internal.UnitSerializer.serialize is called either way to serialize the Response.
I've written a small reproduction here: https://github.com/y-marion/spring-kotlinx-unit-error-repro
The tests for the controller do not work if executed as is, if the testImplementation("io.kotest:kotest-assertions-json:5.9.1") is removed, they work.
The controller also works as intended if the application is started via bootRun.
It’s starting to sound like a way to disable kotlinx is required, perhaps through an annotation that Framework’s codecs and message converters look for or through some mechanism in Boot. It would appear that the classpath alone isn’t a sufficiently strong signal.
@wilkinsona@bclozel Feel free to ping me if you want to brainstorm on what we could do and when.
changed the title [-]Document how to work around edge cases with kotlinx.serialization[/-][+]Add first class support for kotlinx.serialization[/+]on Dec 11, 2024
I did some work on this issue and came to the following conclusion: adding "first class" support for kotlinx.serialization would only be useful if developers want to contribute a custom kotlinx.serialization.json.Json bean and configure it to their liking. This is not what has been requested.
Quite the opposite, it seems that kotlinx serialization conflicts with Jackson when they're both on the classpath. While there was an initial attempt to make the kotlin variant more selective (by looking at annotations), this obviously does not work for Java enums. Switching the order won't improve things: if Kotlin is first, it will handle java enums and @kotlinx.serialization.Serializable-classes, if Jackson is first, it will handle everything.
The only possible improvement in Spring Boot would be remove the Kotlin converter if spring.mvc.converters.preferred-json-mapper=jackson. Still, the default Spring Framework experience is flawed because ordered converters are conflicting with each other. Spring Framework currenty considers Jackson/Gson/Jsonb in this order, configuring a single one to avoid conflicts. I think this should be the same for kotlinx serialization.
I would like to add more context based on my findings and discussions with the Kotlin team.
@hantsy Does not change the fact that spring-projects/spring-framework#34410 is a meaningful change, but FWIW your webflux-json-mapper repro is incorrect. In HelloControllerTest, you try get a Color body while it should be an Array<Color>, hence the error you see. You can do that by using the import org.springframework.test.web.reactive.server.expectBody extension (which will be more easily discoverable once https://youtrack.jetbrains.com/issue/KTIJ-33094 is fixed).
One source of confusion for Kotlin Serialization is that for some use cases, the plugin is not needed (enum), while it is for other ones (mostly build time processing of classes annotated @Serializable to generate a serializer in the project own source code). If the related class has already been processed and is shipped in a dependency, the plugin is not needed. I can see how people could be confused, so better to have an explicit signal to enable Kotlin Serialization in Spring Boot and avoid to have both Jackson and Kotlin Serialization. We did those refinements in Spring Framework 7 milestones, allowing also to fix an annoying limitation since Spring Framework 7 will support open polymorphism with Kotlin Serialization.
Activity
sdeleuze commentedon Nov 25, 2020
Hi, thanks for raising this since indeed I think there is a need for a proper integration for Kotlin serialization in Boot that goes further than what is inherited from Spring Framework.
Current behavior is documented here and has several limitations in Boot context that we would be likely to improve.
The configuration is kind of Spring Framework oriented, a dedicated Boot support similar to JSON-B and related documentation would be much better for consistency.
The second point that should be improved is what you raised in the issue. After discussing with @bclozel I tend to think it is currently not straightforward to support Kotlin serialization and actuators because the web configuration is shared between your app (that wants to use Kotlin serialization) and and actuator (that needs Jackson or similar general purpose JSON library to be configured in your web configuration. And Kotlin serialization is designed to serialize only Kotlin classes annotated with
@Serializableor collection of those.On Framework side, maybe I could refine the converter selection mechanism to have a better support for having both Kotlin Serialization and Jackson via spring-projects/spring-framework#26147 in order to provide a workaround with manual config of both.
On Boot side, after discussing with @snicoll and @bclozel , it sounds like a candidate for Spring Boot 2.5 that would potentially involve the resolution of #20291. As well as dependency management (I am discussing the creation of a Kotlin serialization BOM with the Kotlin team).
dasrida commentedon Nov 26, 2020
Thanks for the detailed response and I'll look forward to future releases!
sdeleuze commentedon Nov 26, 2020
@snicoll After working on a draft commit for spring-projects/spring-framework#26147, I confirm that I plan to configure both Kotlin serialization and Jackson when they are on the classpath, I think that's will make configuration easier and will make actuator and error endpoints still working. Kotlin serialization is more narrow so that seems to work as expected.
So for Boot 2.5, what would be mainly needed is autoconfiguration and documentation à la JSONB, and dependency management with the upcoming BOM that I have asked to Kotlin team (similar to Coroutines one).
[-]Actuator prevents use of KotlinSerializationJsonHttpMessageConverter as default json message converter[/-][+]Add auto-configuration for kotlinx.serialization[/+]snicoll commentedon Nov 26, 2020
Thanks for the follow-up Sébastien. I've repurposed this issue and triaged it for
2.5.x.sdeleuze commentedon Nov 26, 2020
@neostage I have fixed spring-projects/spring-framework#26147 and made sure your use case work fine with Boot 2.4, please test Spring Framework
5.3.2-SNAPSHOTwith both Jackson and Kotlin serialization in the classpath it should work as expected.48 remaining items
y-marion commentedon Oct 31, 2024
We also have a similar issue where we have the dependency on
kotest-assertions-jsonintestImplementation(which brings inkotlinx.serializationas transitive dependency).This causes an error in a
MockMVCtest, where an Endpoint returningResponseEntity<Unit>is called, if we return a ProblemDetail (or any response with a body in case of a failure), askotlinx.serialization.internal.UnitSerializer.serializeis called either way to serialize the Response.I've written a small reproduction here: https://github.com/y-marion/spring-kotlinx-unit-error-repro
The tests for the controller do not work if executed as is, if the
testImplementation("io.kotest:kotest-assertions-json:5.9.1")is removed, they work.The controller also works as intended if the application is started via bootRun.
sdeleuze commentedon Nov 5, 2024
@wilkinsona @bclozel Feel free to ping me if you want to brainstorm on what we could do and when.
[-]Document how to work around edge cases with kotlinx.serialization[/-][+]Add first class support for kotlinx.serialization[/+]philwebb commentedon Dec 11, 2024
We're going to add auto-configuration and look at adding an entry to the
spring.mvc.converters.preferred-json-mapperproperty.bclozel commentedon Feb 11, 2025
I did some work on this issue and came to the following conclusion: adding "first class" support for kotlinx.serialization would only be useful if developers want to contribute a custom
kotlinx.serialization.json.Jsonbean and configure it to their liking. This is not what has been requested.Quite the opposite, it seems that kotlinx serialization conflicts with Jackson when they're both on the classpath. While there was an initial attempt to make the kotlin variant more selective (by looking at annotations), this obviously does not work for Java enums. Switching the order won't improve things: if Kotlin is first, it will handle java enums and
@kotlinx.serialization.Serializable-classes, if Jackson is first, it will handle everything.The only possible improvement in Spring Boot would be remove the Kotlin converter if
spring.mvc.converters.preferred-json-mapper=jackson. Still, the default Spring Framework experience is flawed because ordered converters are conflicting with each other. Spring Framework currenty considers Jackson/Gson/Jsonb in this order, configuring a single one to avoid conflicts. I think this should be the same for kotlinx serialization.I'm closing this issue in favor of spring-projects/spring-framework#34410
sdeleuze commentedon Feb 19, 2025
I would like to add more context based on my findings and discussions with the Kotlin team.
@hantsy Does not change the fact that spring-projects/spring-framework#34410 is a meaningful change, but FWIW your
webflux-json-mapperrepro is incorrect. InHelloControllerTest, you try get aColorbody while it should be anArray<Color>, hence the error you see. You can do that by using theimport org.springframework.test.web.reactive.server.expectBodyextension (which will be more easily discoverable once https://youtrack.jetbrains.com/issue/KTIJ-33094 is fixed).One source of confusion for Kotlin Serialization is that for some use cases, the plugin is not needed (enum), while it is for other ones (mostly build time processing of classes annotated
@Serializableto generate a serializer in the project own source code). If the related class has already been processed and is shipped in a dependency, the plugin is not needed. I can see how people could be confused, so better to have an explicit signal to enable Kotlin Serialization in Spring Boot and avoid to have both Jackson and Kotlin Serialization. We did those refinements in Spring Framework 7 milestones, allowing also to fix an annoying limitation since Spring Framework 7 will support open polymorphism with Kotlin Serialization.As discussed with @bclozel, I think Add kotlinx.serialization as preferred JSON mapper option #44241 is pretty important to allow a smooth migration and/or activation experience.