Skip to content

DataClassRowMapper should not override Kotlin init properties - #26569

Closed
dennie170 wants to merge 1 commit into
spring-projects:mainfrom
dennie170:master
Closed

dennie170 wants to merge 1 commit into
spring-projects:mainfrom
dennie170:master

Conversation

@dennie170

Copy link
Copy Markdown

Since Kotlin data classes are already constructed with values we don't want to override all the properties later on again.
Properties modified in the init { } block in a class would be reverted back to the value of the column again.

An example of this would be setting the default scale of a BigDecimal inside an init block. The init would be called and set the scale after constructing. However, after constructing the BeanPropertyRowMapper would override the BigDecimal again so the scale would be 0 again.

This commit fixes this issue for Kotlin data classes.

Since Kotlin data classes are already constructed with values we
don't want to override all the properties later on again.
Properties modified in the init { } block in a class would be reverted
back to the value of the column again.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 18, 2021
@dennie170

dennie170 commented Jun 28, 2021 •

Copy link
Copy Markdown
Author

Hi team, I see nobody has replies to my issue since February.
Are you gonna have a look at this? It is pretty "breaking" in Kotlin, and now demands some custom solutions.
Thanks!

@sbrannen
sbrannen requested a review from jhoeller June 28, 2021 16:15
@jhoeller jhoeller self-assigned this Jul 6, 2021
@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug labels Jul 6, 2021
@jhoeller jhoeller modified the milestones: 5.3.10, 5.3.9 Jul 6, 2021
@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 8, 2021
@jhoeller jhoeller changed the title Fixed bug that would override Kotlin init properties DataClassRowMapper should not override Kotlin init properties Jul 8, 2021
@jhoeller jhoeller closed this in 4fe3ca1 Jul 9, 2021
@jhoeller

jhoeller commented Jul 9, 2021

Copy link
Copy Markdown
Contributor

It turns out that Kotlin var declarations implicitly generate setter methods for the affected properties, which leads to our DataClassRowMapper populating that setter as well (since we're expecting either a constructor argument or a setter method, not both, we haven't been checking for this explicitly). Mixing and matching between constructor arguments and setter methods is allowed by design, so we can't cut off the latter completely (like the original PR does). Instead, I've revised the DataClassRowMapper algorithm to suppress setter method calls for all properties that have been populated through constructor arguments already. In addition to a Java-based test for this behavior, I've also added your Kotlin test case.

Thanks for the PR, in any case!

@dennie170

Copy link
Copy Markdown
Author

Thanks for figuring it out and the clear explanation! Glad to see it's fixed now.

lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this pull request Mar 26, 2022
sbrannen pushed a commit that referenced this pull request Oct 30, 2026
SimplePropertyRowMapper maps the remaining columns via setters or
fields after instantiating the target class. It skips the columns
used for constructor arguments by index, but findColumn() returns the
first column for a given label. If a label occurs more than once, as
with "select *" over a join, the other column with that label is
mapped again onto the property that the constructor already set.

For a record, this fails with an IllegalStateException since the
field is final. For a class with final fields, the constructor value
is silently overwritten with the value of the other column.

Skip any setter or field whose property name matches a constructor
parameter, as documented in the class-level Javadoc and as done by
DataClassRowMapper. The check runs before the write method parameter
is resolved, so that a read-only constructor property does not fail
either.

See gh-26569
Closes gh-37602

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants