Skip to content

When using Kotlin, mutable super-class's properties are not bound when a sub-class declares a non-default primary constructor that does not cover the super-class's properties #44849

Open
@vominhtri231

Description

@vominhtri231

I have a spring boot - Kotlin project, which I am upgrading from Spring 5, Java 8 to Spring 6, Java 21

build.gradle.kts

plugins {
    kotlin("jvm") version "2.1.20"
    id("org.springframework.boot") version "3.4.4"
    id("org.jetbrains.kotlin.plugin.spring") version "2.1.20"
    id("io.spring.dependency-management") version "1.1.7"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation(kotlin("reflect"))
}

MyProperties.kt

@ConfigurationProperties(prefix = "my.test")
@Component
class MyProperties {
    var customs: List<CustomA> = listOf()
    var bases: List<Base> = listOf()

    open class Base(
        var type: String = ""
    )

    class CustomA(
        var y: String = "",
        var z: String = "",
    ) : Base()
}

Run the application by bootRun task, with these env variables:

MY_TEST_BASES_0_TYPE=value4;MY_TEST_CUSTOMS_0_TYPE=value3;MY_TEST_CUSTOMS_0_Y=value1;MY_TEST_CUSTOMS_0_Z=value2

I would expect that the myProperties.customs[0].type would return value3, but it would return an empty string instead.

The problem only happens if the kotlin("reflect") dependency exists and does not occur in Spring 5, Java 8.

It looks like the base class's properties are not bound at all. If I modify the MyProperties class as this, the type property will bind correctly

open class Base(
   open var type: String = ""
)

class CustomA(
   override var type: String = "",
   var y: String = "",
   var z: String = "",
) : Base()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions