Description

I encountered an issue with BeanUtils.copyProperties() in Spring Framework where it does not correctly copy enum fields in a Kotlin data class. Below is a minimal reproducible example that demonstrates the problem.

@SpringBootTest
class ApplicationTests {

    @Test
    fun `should copy enum fields with BeanUtils`() {
        val source= User(1, "John", UserRole.ADMIN)
        val target = User(0, "")
        BeanUtils.copyProperties(source, target)
        assert(target.role == source.role)  // This fails
    }
}

data class User(
    val id: Long,
    val name: String,
    val role: UserRole = UserRole.USER
)

enum class UserRole {
    USER, ADMIN, CUSTOMER
}

Expected Behaviour

The role field in target should be UserRole.ADMIN after calling BeanUtils.copyProperties(source, target).

Actual Behavior:

The role field in target remains UserRole.USER, indicating that the enum field was not copied correctly.

Environment:

Spring Framework version: 6.1.1

Comment From: jhoeller

You need to declare those data class fields as var instead of val, otherwise Kotlin does not generate Java setter methods at all. In your case, it's not just the enum field but rather none of those fields getting copied.

Comment From: takkiraz

Thank you for the clarification! I actually noticed the mistake right after creating the ticket—sorry for the unnecessary noise. Lesson learned—var it is!