Skip to content

Commit

Permalink
Fixes mybatis#67 BindingException is thrown when a collection which c…
Browse files Browse the repository at this point in the history
…ontains null is passed as the 'collection' of foreach tag.
  • Loading branch information
harawata committed Jun 22, 2013
1 parent 02f9b9c commit 3b9e7f4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean hasGetter(String name) {
if (map.containsKey(prop.getIndexedName())) {
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
return map.containsKey(name);
return true;
} else {
return metaValue.hasGetter(prop.getChildren());
}
Expand Down
26 changes: 25 additions & 1 deletion src/test/java/org/apache/ibatis/reflection/MetaObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -241,7 +242,30 @@ public void shouldDemonstrateDeeplyNestedMapProperties() {
assertEquals("Clinton", name.get("first"));
assertEquals("1 Some Street", address.get("street"));
}


@Test
public void shouldDemonstrateNullValueInMap() {
HashMap<String, String> map = new HashMap<String, String>();
MetaObject metaMap = SystemMetaObject.forObject(map);
assertFalse(metaMap.hasGetter("phone.home"));

metaMap.setValue("phone", null);
assertTrue(metaMap.hasGetter("phone"));
// hasGetter returns true if the parent exists and is null.
assertTrue(metaMap.hasGetter("phone.home"));
assertTrue(metaMap.hasGetter("phone.home.ext"));
assertNull(metaMap.getValue("phone"));
assertNull(metaMap.getValue("phone.home"));
assertNull(metaMap.getValue("phone.home.ext"));

metaMap.setValue("phone.office", "789");
assertFalse(metaMap.hasGetter("phone.home"));
assertFalse(metaMap.hasGetter("phone.home.ext"));
assertEquals("789", metaMap.getValue("phone.office"));
assertNotNull(metaMap.getValue("phone"));
assertNull(metaMap.getValue("phone.home"));
}

@Test
public void shouldNotUseObjectWrapperFactoryByDefault() {
MetaObject meta = SystemMetaObject.forObject(new Product());
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/foreach/ForEachTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,23 @@ public void shouldHandleComplexNullItem() {
}
}

@Test
public void shouldHandleMoreComplexNullItem() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user1 = new User();
User bestFriend = new User();
bestFriend.setId(5);
user1.setBestFriend(bestFriend);
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(null);
int count = mapper.countByBestFriend(users);
Assert.assertEquals(1, count);
} finally {
sqlSession.close();
}
}

}
2 changes: 2 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/foreach/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface Mapper {

int countByUserList(List<User> users);

int countByBestFriend(List<User> users);

}
10 changes: 10 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/foreach/Mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@
</where>
</select>

<select id="countByBestFriend" resultType="_int" parameterType="list">
select count(*) from users
<where>
id in
<foreach item="item" collection="list" separator="," open="(" close=")">
#{item.bestFriend.id, jdbcType=NUMERIC}
</foreach>
</where>
</select>

</mapper>
9 changes: 9 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/foreach/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class User {

private Integer id;
private String name;
private User bestFriend;
private List<User> friendList;

public Integer getId() {
Expand All @@ -39,6 +40,14 @@ public void setName(String name) {
this.name = name;
}

public User getBestFriend() {
return bestFriend;
}

public void setBestFriend(User bestFriend) {
this.bestFriend = bestFriend;
}

public List<User> getFriendList() {
return friendList;
}
Expand Down

0 comments on commit 3b9e7f4

Please sign in to comment.