-
Notifications
You must be signed in to change notification settings - Fork 40.7k
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
Document that configuration property binding to a Kotlin value class with a default is not supported #41693
Comments
This issue also happens when value class in configuration properties and any properties have default values, not just value class property: @JvmInline
value class EntityId(val value: Int)
@ConfigurationProperties(prefix = "test")
data class AppConfig(
val entityId: EntityId,
val anotherProperty: String = "default value",
) In this case if |
Reproducer for @FredoNook's case: package com.example
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
@JvmInline
value class EntityId(val value: Int)
@ConfigurationProperties(prefix = "test")
data class AppConfig(
val entityId: EntityId,
val anotherProperty: String = "default value",
)
@SpringBootApplication
@EnableConfigurationProperties(AppConfig::class)
class TestApp {
@Bean
fun runner(config: AppConfig) = CommandLineRunner {
println("Value: ${config.entityId}")
}
}
fun main(args: Array<String>) {
System.setProperty(
"SPRING_APPLICATION_JSON",
"""
{
"test": {
"entityId": 1
}
}
""".trimIndent()
)
runApplication<TestApp>(*args)
} |
As far as I can tell, there's nothing that we can do about this as it doesn't appear to be possible to create an instance of Viewed from Java,
Only one of these, the first, returns a As noted above, this works fine for a non-value class:
In this case In summary, this problem appears to be a limitation of how a Kotlin value class is mapped into Java. You may want to open a Kotlin issue to see if support for working with value classes from Java can be improved. Until then, I don't think there's anything that we can do here other than documenting the limitation. We can use this issue to add something here. |
Affects: Spring Boot 3.3.2, Spring 6.1.11
When using a value class property in
@ConfigurationProperties
with a default value and not providing its value in application configuration leads to an error during binding of configuration. (Tested with Kotlin 1.9.25.)Reproducer:
Running this fails with:
Ideally, this would work just like it works with defaults for non-value classes.
The text was updated successfully, but these errors were encountered: