Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search readable property when resolving constructor arg type by name #2804

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private Class<?> resolveResultJavaType(Class<?> resultType, String property, Cla
if (javaType == null && property != null) {
try {
MetaClass metaResultType = MetaClass.forClass(resultType, configuration.getReflectorFactory());
javaType = metaResultType.getSetterType(property);
javaType = metaResultType.getGetterType(property);
} catch (Exception e) {
// ignore, following null check statement will deal with the situation
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public ResultMap build() {
final List<String> actualArgNames = argNamesOfMatchingConstructor(constructorArgNames);
if (actualArgNames == null) {
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
+ resultMap.getType().getName() + "' by arg names " + constructorArgNames
+ ". There might be more info in debug log.");
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
+ ". Note that 'javaType' is required when there is no readable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
}
resultMap.constructorResultMappings.sort((o1, o2) -> {
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public interface RecordTypeMapper {
@Select("select val, id, url from prop where id = #{id}")
Property selectProperty(int id);

@Arg(name = "id", column = "id", id = true)
@Arg(name = "value", column = "val")
@Arg(name = "URL", column = "url")
@Select("select val, id, url from prop where id = #{id}")
Property selectPropertyNoJavaType(int id);

@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
int insertProperty(Property property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ void testSelectRecord() {
}
}

@Test
void shouldResolveConstructorArgType() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
Property prop = mapper.selectPropertyNoJavaType(1);
assertEquals("Val1!", prop.value());
assertEquals("https://www.google.com", prop.URL());
}
}

@Test
void testSelectRecordAutomapping() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down