Skip to content

Commit 3b9e7f4

Browse files
committed
Fixes #67 BindingException is thrown when a collection which contains null is passed as the 'collection' of foreach tag.
1 parent 02f9b9c commit 3b9e7f4

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

src/main/java/org/apache/ibatis/reflection/wrapper/MapWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public boolean hasGetter(String name) {
109109
if (map.containsKey(prop.getIndexedName())) {
110110
MetaObject metaValue = metaObject.metaObjectForProperty(prop.getIndexedName());
111111
if (metaValue == SystemMetaObject.NULL_META_OBJECT) {
112-
return map.containsKey(name);
112+
return true;
113113
} else {
114114
return metaValue.hasGetter(prop.getChildren());
115115
}

src/test/java/org/apache/ibatis/reflection/MetaObjectTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertNotNull;
2021
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertTrue;
2223

@@ -241,7 +242,30 @@ public void shouldDemonstrateDeeplyNestedMapProperties() {
241242
assertEquals("Clinton", name.get("first"));
242243
assertEquals("1 Some Street", address.get("street"));
243244
}
244-
245+
246+
@Test
247+
public void shouldDemonstrateNullValueInMap() {
248+
HashMap<String, String> map = new HashMap<String, String>();
249+
MetaObject metaMap = SystemMetaObject.forObject(map);
250+
assertFalse(metaMap.hasGetter("phone.home"));
251+
252+
metaMap.setValue("phone", null);
253+
assertTrue(metaMap.hasGetter("phone"));
254+
// hasGetter returns true if the parent exists and is null.
255+
assertTrue(metaMap.hasGetter("phone.home"));
256+
assertTrue(metaMap.hasGetter("phone.home.ext"));
257+
assertNull(metaMap.getValue("phone"));
258+
assertNull(metaMap.getValue("phone.home"));
259+
assertNull(metaMap.getValue("phone.home.ext"));
260+
261+
metaMap.setValue("phone.office", "789");
262+
assertFalse(metaMap.hasGetter("phone.home"));
263+
assertFalse(metaMap.hasGetter("phone.home.ext"));
264+
assertEquals("789", metaMap.getValue("phone.office"));
265+
assertNotNull(metaMap.getValue("phone"));
266+
assertNull(metaMap.getValue("phone.home"));
267+
}
268+
245269
@Test
246270
public void shouldNotUseObjectWrapperFactoryByDefault() {
247271
MetaObject meta = SystemMetaObject.forObject(new Product());

src/test/java/org/apache/ibatis/submitted/foreach/ForEachTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,23 @@ public void shouldHandleComplexNullItem() {
8888
}
8989
}
9090

91+
@Test
92+
public void shouldHandleMoreComplexNullItem() {
93+
SqlSession sqlSession = sqlSessionFactory.openSession();
94+
try {
95+
Mapper mapper = sqlSession.getMapper(Mapper.class);
96+
User user1 = new User();
97+
User bestFriend = new User();
98+
bestFriend.setId(5);
99+
user1.setBestFriend(bestFriend);
100+
List<User> users = new ArrayList<User>();
101+
users.add(user1);
102+
users.add(null);
103+
int count = mapper.countByBestFriend(users);
104+
Assert.assertEquals(1, count);
105+
} finally {
106+
sqlSession.close();
107+
}
108+
}
109+
91110
}

src/test/java/org/apache/ibatis/submitted/foreach/Mapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public interface Mapper {
2323

2424
int countByUserList(List<User> users);
2525

26+
int countByBestFriend(List<User> users);
27+
2628
}

src/test/java/org/apache/ibatis/submitted/foreach/Mapper.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@
4040
</where>
4141
</select>
4242

43+
<select id="countByBestFriend" resultType="_int" parameterType="list">
44+
select count(*) from users
45+
<where>
46+
id in
47+
<foreach item="item" collection="list" separator="," open="(" close=")">
48+
#{item.bestFriend.id, jdbcType=NUMERIC}
49+
</foreach>
50+
</where>
51+
</select>
52+
4353
</mapper>

src/test/java/org/apache/ibatis/submitted/foreach/User.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class User {
2121

2222
private Integer id;
2323
private String name;
24+
private User bestFriend;
2425
private List<User> friendList;
2526

2627
public Integer getId() {
@@ -39,6 +40,14 @@ public void setName(String name) {
3940
this.name = name;
4041
}
4142

43+
public User getBestFriend() {
44+
return bestFriend;
45+
}
46+
47+
public void setBestFriend(User bestFriend) {
48+
this.bestFriend = bestFriend;
49+
}
50+
4251
public List<User> getFriendList() {
4352
return friendList;
4453
}

0 commit comments

Comments
 (0)