You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
classApplicationTests {
@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 classUser(
valid:Long,
valname:String,
valrole:UserRole = UserRole.USER
)
enumclassUserRole {
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
The text was updated successfully, but these errors were encountered:
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.
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!
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.
Expected Behaviour
The role field in
target
should beUserRole.ADMIN
after callingBeanUtils.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
The text was updated successfully, but these errors were encountered: