Skip to content

Conversation

dmitrysulman
Copy link
Contributor

@dmitrysulman dmitrysulman commented Jul 25, 2025

This PR adds auto-configuration support for Kotlin Serialization in Spring Boot, addressing spring-projects/spring-boot#44241

Summary of Changes

  1. New Module: Introduced a new module, spring-boot-kotlin-serialization, which provides KotlinSerializationAutoConfiguration. This auto-configuration supports a set of new properties under the spring.kotlin-serialization prefix for customizing the Json instance from kotlinx.serialization.
  2. New Starter: Added a corresponding starter module spring-boot-starter-kotlin-serialization.
  3. HTTP Message Converters Integration:
  • Introduced KotlinSerializationHttpMessageConvertersConfiguration in spring-boot-http-converter.
  • Added support for a new kotlin-serialization option to the spring.http.converters.preferred-json-mapper property to expose KotlinSerializationJsonHttpMessageConverter as a bean.
  • Updated the HttpMessageConverters configuration to add KotlinSerializationJsonHttpMessageConverter immediately before MappingJackson2HttpMessageConverter. This restores the behavior prior to the Spring Framework change when spring.http.converters.preferred-json-mapper=kotlin-serialization is explicitly set.
  1. Documentation: Updated relevant JSON feature documentation to include the new kotlin-serialization option and configuration support.
  2. Test Coverage: All newly introduced logic is fully covered by tests.

There was a suggestion in the original issue to use kotlinx as the option name. However, kotlinx is a general namespace used across many Kotlin libraries (like kotlinx.coroutines, kotlinx-datetime, kotlinx-io), not just for serialization. To avoid confusion and be more specific, I chose to use kotlin-serialization both in the code (KotlinSerialization* classes) and as the value for the spring.http.converters.preferred-json-mapper property. This keeps the naming clear and consistent.

Let me know if anything needs to be changed or improved, happy to adjust things if needed.

Fixes #44241

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 25, 2025
testRuntimeOnly("org.jetbrains.kotlin:kotlin-reflect")
}

tasks.named("checkAutoConfigurationClasses", CheckAutoConfigurationClasses.class) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a workaround to prevent the CheckAutoConfigurationClasses task from failing. The issue is that the Kotlin Gradle Plugin adds a kotlin SourceDirectorySet to the main SourceSet even when there are no Kotlin source files. As a result, the CheckAutoConfigurationClasses task throws an IOException when it tries to walk through the /build/classes/kotlin/main directory, which doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the ideal solution, it would be better to address the issue in the CheckAutoConfigurationClasses task itself by filtering out the Kotlin directory from the scan.

@dmitrysulman dmitrysulman force-pushed the kotlin-serialization-auto-configuration branch 4 times, most recently from 78f9e3b to 5928326 Compare July 31, 2025 12:50
@dmitrysulman
Copy link
Contributor Author

@bclozel just wanted to check if this PR reflects what was proposed in #44241? I’d be glad to adjust it if something needs to be changed.

@dmitrysulman dmitrysulman force-pushed the kotlin-serialization-auto-configuration branch from 5928326 to 7d141fb Compare September 2, 2025 15:17
@dmitrysulman dmitrysulman force-pushed the kotlin-serialization-auto-configuration branch from 7d141fb to cdbbc8c Compare September 2, 2025 16:12
@bclozel bclozel self-assigned this Sep 2, 2025
@bclozel bclozel added type: enhancement A general enhancement theme: kotlin and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 10, 2025
@bclozel bclozel added this to the 4.0.0-M3 milestone Sep 10, 2025
bclozel pushed a commit that referenced this pull request Sep 10, 2025
@bclozel bclozel closed this in 6a55d7b Sep 10, 2025
@bclozel
Copy link
Member

bclozel commented Sep 10, 2025

Thanks for your contribution @dmitrysulman , this is now merged in the main branch and will be released with our next milestone.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add kotlinx.serialization as preferred JSON mapper option

3 participants