Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Introduce Kotlin Serialization auto-configuration #46546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dmitrysulman wants to merge 1 commit into spring-projects:main
base: main
Choose a base branch
Loading
from dmitrysulman:kotlin-serialization-auto-configuration

Conversation

Copy link
Contributor

@dmitrysulman dmitrysulman commented Jul 25, 2025
edited
Loading

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

testRuntimeOnly("org.jetbrains.kotlin:kotlin-reflect")
}

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

@dmitrysulman dmitrysulman Jul 25, 2025

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

@dmitrysulman dmitrysulman Jul 29, 2025

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
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
See spring-projects#44241
Signed-off-by: Dmitry Sulman <dmitry.sulman@gmail.com>
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Add kotlinx.serialization as preferred JSON mapper option

AltStyle によって変換されたページ (->オリジナル) /