Skip to content

Commit fb89c55

Browse files
committed
Fixes #343 Accessing dynamic variable could cause BindingException when null is bound.
1 parent bdb33da commit fb89c55

File tree

8 files changed

+101
-1
lines changed

8 files changed

+101
-1
lines changed

src/main/java/org/apache/ibatis/scripting/xmltags/DynamicContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Object getProperty(Map context, Object target, Object name)
106106
Map map = (Map) target;
107107

108108
Object result = map.get(name);
109-
if (result != null) {
109+
if (map.containsKey(name) || result != null) {
110110
return result;
111111
}
112112

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*-
2+
* Copyright 2009-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.apache.ibatis.submitted.dynsql;
18+
19+
import org.apache.ibatis.annotations.Param;
20+
21+
public interface DynSqlMapper {
22+
String selectDescription(@Param("p") String p);
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2015 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE mapper
21+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
23+
24+
<mapper namespace="org.apache.ibatis.submitted.dynsql.DynSqlMapper">
25+
<select id="selectDescription" resultType="string">
26+
<bind name="condition" value="p" />
27+
SELECT description
28+
FROM ibtest.names
29+
<if test="condition == null">
30+
WHERE id = 3
31+
</if>
32+
</select>
33+
</mapper>

src/test/java/org/apache/ibatis/submitted/dynsql/DynSqlTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,16 @@ public void testOgnlStaticMethodCall() {
158158
}
159159
}
160160

161+
@Test
162+
public void testBindNull() {
163+
SqlSession sqlSession = sqlSessionFactory.openSession();
164+
try {
165+
DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
166+
String description = mapper.selectDescription(null);
167+
assertEquals("Pebbles", description);
168+
} finally {
169+
sqlSession.close();
170+
}
171+
}
172+
161173
}

src/test/java/org/apache/ibatis/submitted/dynsql/MapperConfig.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
<mappers>
4040
<mapper resource="org/apache/ibatis/submitted/dynsql/DynSql.xml"/>
41+
<mapper class="org.apache.ibatis.submitted.dynsql.DynSqlMapper" />
4142
</mappers>
4243

4344
</configuration>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,21 @@ public void shouldHandleMoreComplexNullItem() {
107107
}
108108
}
109109

110+
@Test
111+
public void nullItemInContext() {
112+
SqlSession sqlSession = sqlSessionFactory.openSession();
113+
try {
114+
Mapper mapper = sqlSession.getMapper(Mapper.class);
115+
User user1 = new User();
116+
user1.setId(3);
117+
List<User> users = new ArrayList<User>();
118+
users.add(user1);
119+
users.add(null);
120+
String name = mapper.selectWithNullItemCheck(users);
121+
Assert.assertEquals("User3", name);
122+
} finally {
123+
sqlSession.close();
124+
}
125+
}
126+
110127
}

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

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

2626
int countByBestFriend(List<User> users);
2727

28+
String selectWithNullItemCheck(List<User> users);
29+
2830
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,16 @@
5050
</where>
5151
</select>
5252

53+
<select id="selectWithNullItemCheck" resultType="string">
54+
select name from users
55+
<where>
56+
id in
57+
<foreach item="item" collection="list" separator="," open="(" close=")">
58+
<if test="item != null">
59+
#{item.id, jdbcType=NUMERIC}
60+
</if>
61+
</foreach>
62+
</where>
63+
</select>
64+
5365
</mapper>

0 commit comments

Comments
 (0)