Skip to content

fix Use constructor element and discriminator case element is resultMap property NPE #2353

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public ResultMap build() {
if (resultMap.idResultMappings.isEmpty()) {
resultMap.idResultMappings.addAll(resultMap.resultMappings);
}
if (!constructorArgNames.isEmpty()) {
if (!constructorArgNames.isEmpty() && resultMap.type != null) {
final List<String> actualArgNames = argNamesOfMatchingConstructor(constructorArgNames);
if (actualArgNames == null) {
throw new BuilderException("Error in result map '" + resultMap.id
Expand Down
45 changes: 44 additions & 1 deletion src/test/java/org/apache/ibatis/builder/BlogMapper.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2019 the original author or authors.
Copyright 2009-2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,6 +94,15 @@
</discriminator>
</resultMap>

<resultMap id="joinedConstructorPost" type="org.apache.ibatis.domain.blog.Post">
<constructor>
<idArg column="post_id" name="id"/>
</constructor>
<discriminator javaType="int" column="draft">
<case value="1" resultMap="draftPost"/>
</discriminator>
</resultMap>

<resultMap id="draftPost" type="org.apache.ibatis.domain.blog.DraftPost" extends="joinedPost"/>

<resultMap id="blogJoinedWithPostsAndAuthor" type="Blog">
Expand Down Expand Up @@ -149,6 +158,40 @@
where B.id = #{id}
</select>

<select id="selectBlogJoinedConstructorWithPostsAndAuthor" parameterType="int" resultMap="joinedConstructorPost">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>

<select id="selectAllPosts" resultType="hashmap">
select * from post order by id
</select>
Expand Down
45 changes: 44 additions & 1 deletion src/test/java/org/apache/ibatis/builder/xsd/BlogMapper.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2019 the original author or authors.
Copyright 2009-2021 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,6 +93,15 @@
</discriminator>
</resultMap>

<resultMap id="joinedConstructorPost" type="org.apache.ibatis.domain.blog.Post">
<constructor>
<idArg column="post_id" name="id"/>
</constructor>
<discriminator javaType="int" column="draft">
<case value="1" resultMap="draftPost"/>
</discriminator>
</resultMap>

<resultMap id="draftPost" type="org.apache.ibatis.domain.blog.DraftPost" extends="joinedPost"/>

<resultMap id="blogJoinedWithPostsAndAuthor" type="Blog">
Expand Down Expand Up @@ -148,6 +157,40 @@
where B.id = #{id}
</select>

<select id="selectBlogJoinedConstructorWithPostsAndAuthor" parameterType="int" resultMap="joinedConstructorPost">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>

<select id="selectAllPosts" resultType="hashmap">
select * from post order by id
</select>
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/apache/ibatis/domain/blog/DraftPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
package org.apache.ibatis.domain.blog;

public class DraftPost extends Post {

public DraftPost() {
}

public DraftPost(int id) {
super(id);
}
}
7 changes: 7 additions & 0 deletions src/test/java/org/apache/ibatis/domain/blog/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public class Post {
private List<Comment> comments;
private List<Tag> tags;

public Post() {
}

public Post(int id) {
this.id = id;
}

public int getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface BlogMapper {

List<Map> selectAllPosts(RowBounds rowBounds, Object param);

List<Map> selectBlogJoinedConstructorWithPostsAndAuthor(int id);

}
9 changes: 9 additions & 0 deletions src/test/java/org/apache/ibatis/session/SqlSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@ void shouldLimitResultsUsingMapperClass() {
}
}

@Test
void shouldNotNPEResultsUsingMapperClass() {
try (SqlSession session = sqlMapper.openSession()) {
BlogMapper mapper = session.getMapper(BlogMapper.class);
List<Map> postsAndAuthor = mapper.selectBlogJoinedConstructorWithPostsAndAuthor(1);
assertNotNull(postsAndAuthor);
}
}

private static class TestResultHandler implements ResultHandler {
int count = 0;
@Override
Expand Down