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
Constructor binding of @ConfigurationProperties to a lateinit property fails with kotlin.UninitializedPropertyAccessException #35603
Comments
@ConfigurationProperties
to a Kotlin data class requires @ConstructorBinding
, doesn't work at all when class defines default values
@piotrp have you reviewed the migration guide, in particular this section? Also, a |
Yes, and this indicates that |
Then it's a misuse of Spring Boot API, and previously it just happened to work? |
No, I don't think so. Stephane's comment about mixing |
This appears to be a regression in Spring Boot 3.0.5. Here's a minimal reproducer: package com.example.gh35603
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
@SpringBootApplication
@EnableConfigurationProperties(RedisConfigurationProperties::class)
class Gh35603Application
fun main(args: Array<String>) {
runApplication<Gh35603Application>("--redis.redis1.host=test")
}
@ConfigurationProperties("redis")
class RedisConfigurationProperties {
lateinit var redis1: RedisProperties
lateinit var redis2: RedisProperties
data class RedisProperties(val host: String = "x", val port: Int = 0)
} With 3.0.4, the application starts:
It also works with 2.7.12 with With 3.0.5 it fails:
|
@ConfigurationProperties
to a Kotlin data class requires @ConstructorBinding
, doesn't work at all when class defines default values
The problem with |
This doesn't appear to be possible through Kotlin's Java reflection support. While we can get a |
I'm updating my application from Spring Boot 2.7 to 3.1, and I found what looks to be a resurrection/continuation of bug #32416:
@ConfigurationProperties
combined with Kotlin'sdata class
fails in some scenarios.Common test code:
1. Data class without default values
Spring Boot 2.7.12: works
Spring Boot 3.1.0:
@ConstructorBinding
is no longer valid here, removing it causeskotlin.UninitializedPropertyAccessException: lateinit property redis1 has not been initialized
2. Data class with default values
Spring Boot 2.7.12: works
Spring Boot 3.1.0:
@ConstructorBinding
is no longer valid here, removing it causeskotlin.UninitializedPropertyAccessException: lateinit property redis1 has not been initialized
@ConstructorBinding
to constructor:java.lang.IllegalStateException: com.example.demo.RedisProperties declares @ConstructorBinding on a no-args constructor
."Workaround": remove default value from at least one field.
The text was updated successfully, but these errors were encountered: