mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.74k stars 12.84k forks source link

Regression: Constructor args fails on record types #2840

Open jdmichal opened 1 year ago

jdmichal commented 1 year ago

MyBatis version

3.5.11 via mybatis-spring-boot-starter 2.3.0

Regression from 3.5.9 via mybatis-spring-boot-starter 2.2.2

Database vendor and version

PostgreSQL 14.x

Test case or example project

public record ResultType(
    String value) {
}
<resultMap id="resultTypeMap" type="ResultType">
    <constructor>
        <arg column="value" name="value" />
    </constructor>
</resultMap>

<select id="getResult" resultMap="resultTypeMap">
    SELECT 'foobar' AS value;
</select>

Steps to reproduce

  1. Create a record type and map to it via a constructor resultMap, as above.

Expected result

The default record constructor would be found and linked.

Actual result

The default record constructor is not found due to the java type of the ResultMapping being java.lang.Object.

Workaround

Define the javaType for the constructor args. This was not necessary in version 3.5.9.

jdmichal commented 1 year ago

It appears that this is a duplicate of the closed #2803. However, it appears the PR that closed that issue is being withdrawn. So perhaps having this new issue open or closing this and reopening 2803 is appropriate.

As mentioned by @tzie0062 in that issue, this is a regression from 3.5.9 to 3.5.11. Some explanation seems in order for why this regression is taking place in a patch release, regardless of the attempt of #2804 to fix it.

harawata commented 1 year ago

Hello @jdmichal ,

It is the same issue as #2803 . Here is the background.

It worked in 3.5.9 because the value was recognized as a writable property (which was clearly wrong). It caused a problem with auto-mapping ( #2195 ), so we fixed it in 3.5.10. As a result, value is no longer recognized as a writable property.

Now, as documented, in constructor mapping, javaType is required when there is no (writable) property with the same name and type. This is why your result map stopped working in 3.5.10+ (it may work in 3.5.12, but it is just another 'accident' 😣 and we will release 3.5.13 ASAP).

It is unfortunate that it accidentally worked in ≤ 3.5.9 and I apologize for the confusion, but the right solution to your problem is to specify javaType instead of name.

This is going to be a FAQ, I imagine... 😔