Skip to content

Add first class support for kotlinx.serialization #24238

Closed as not planned
Closed as not planned

Description

@dasrida

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.

Activity

  1. sdeleuze commented on Nov 25, 2020

    @sdeleuze
    Contributor

    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).

  2. dasrida commented on Nov 26, 2020

    @dasrida
    Author

    Thanks for the detailed response and I'll look forward to future releases!

  3. sdeleuze commented on Nov 26, 2020

    @sdeleuze
    Contributor

    @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).

  4. changed the title [-]Actuator prevents use of KotlinSerializationJsonHttpMessageConverter as default json message converter[/-] [+]Add auto-configuration for kotlinx.serialization[/+] on Nov 26, 2020
  5. added and removed
    status: waiting-for-triageAn issue that has not yet been triaged. Contributions will not be accepted.
    on Nov 26, 2020
  6. added this to the 2.5.x milestone on Nov 26, 2020
  7. snicoll commented on Nov 26, 2020

    @snicoll
    Member

    Thanks for the follow-up Sébastien. I've repurposed this issue and triaged it for 2.5.x.

  8. sdeleuze commented on Nov 26, 2020

    @sdeleuze
    Contributor

    @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.

  9. added
    status: blockedAn issue that's blocked on an external project change
    and removed
    status: blockedAn issue that's blocked on an external project change
    on Mar 15, 2021
  10. removed this from the 2.5.x milestone on Mar 19, 2021
  11. 48 remaining items

  12. y-marion commented on Oct 31, 2024

    @y-marion

    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.

  13. sdeleuze commented on Nov 5, 2024

    @sdeleuze
    Contributor

    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.

  14. modified the milestones: 3.x, 3.5.x on Dec 11, 2024
  15. added and removed
    for: team-meetingAn issue we'd like to discuss as a team to make progress
    on Dec 11, 2024
  16. changed the title [-]Document how to work around edge cases with kotlinx.serialization[/-] [+]Add first class support for kotlinx.serialization[/+] on Dec 11, 2024
  17. philwebb commented on Dec 11, 2024

    @philwebb
    Member

    We're going to add auto-configuration and look at adding an entry to the spring.mvc.converters.preferred-json-mapper property.

  18. bclozel commented on Feb 11, 2025

    @bclozel
    Member

    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'm closing this issue in favor of spring-projects/spring-framework#34410

  19. removed this from the 3.5.x milestone on Feb 11, 2025
  20. sdeleuze commented on Feb 19, 2025

    @sdeleuze
    Contributor

    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.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fixstatus: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址