Skip to content

Commit a56c38d

Browse files
committed
Fixes #65 Property reference breaks when its name partly matches with 'item' or 'index' variable.
1 parent a1f3f0a commit a56c38d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ public String getSql() {
145145
public void appendSql(String sql) {
146146
GenericTokenParser parser = new GenericTokenParser("#{", "}", new TokenHandler() {
147147
public String handleToken(String content) {
148-
String newContent = content.replaceFirst(item, itemizeItem(item, index));
148+
String newContent = content.replaceFirst("^\\s*" + item + "(?![^.,:\\s])", itemizeItem(item, index));
149149
if (itemIndex != null && newContent.equals(content)) {
150-
newContent = content.replaceFirst(itemIndex, itemizeItem(itemIndex, index));
150+
newContent = content.replaceFirst("^\\s*" + itemIndex + "(?![^.,:\\s])", itemizeItem(itemIndex, index));
151151
}
152152
return new StringBuilder("#{").append(newContent).append("}").toString();
153153
}

src/test/java/org/apache/ibatis/builder/xml/dynamic/DynamicSqlSourceTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
2525
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
2628

2729
import org.apache.ibatis.BaseDataTest;
2830
import org.apache.ibatis.io.Resources;
@@ -289,6 +291,28 @@ public void shouldIterateOnceForEachItemInCollection() throws Exception {
289291
assertEquals("__frch_item_2", boundSql.getParameterMappings().get(2).getProperty());
290292
}
291293

294+
@Test
295+
public void shouldPerformStrictMatchOnForEachVariableSubstitution() throws Exception {
296+
final Map<String, Object> param = new HashMap<String, Object>();
297+
final Map<String, String> uuu = new HashMap<String, String>();
298+
uuu.put("u", "xyz");
299+
List<Bean> uuuu = new ArrayList<Bean>();
300+
uuuu.add(new Bean("bean id"));
301+
param.put("uuu", uuu);
302+
param.put("uuuu", uuuu);
303+
DynamicSqlSource source = createDynamicSqlSource(
304+
new TextSqlNode("INSERT INTO BLOG (ID, NAME, NOTE, COMMENT) VALUES"),
305+
new ForEachSqlNode(new Configuration(),mixedContents(
306+
new TextSqlNode("#{uuu.u}, #{u.id}, #{ u,typeHandler=org.apache.ibatis.type.StringTypeHandler},"
307+
+ " #{u:VARCHAR,typeHandler=org.apache.ibatis.type.StringTypeHandler}")), "uuuu", "uu", "u", "(", ")", ","));
308+
BoundSql boundSql = source.getBoundSql(param);
309+
assertEquals(4, boundSql.getParameterMappings().size());
310+
assertEquals("uuu.u", boundSql.getParameterMappings().get(0).getProperty());
311+
assertEquals("__frch_u_0.id", boundSql.getParameterMappings().get(1).getProperty());
312+
assertEquals("__frch_u_0", boundSql.getParameterMappings().get(2).getProperty());
313+
assertEquals("__frch_u_0", boundSql.getParameterMappings().get(3).getProperty());
314+
}
315+
292316
private DynamicSqlSource createDynamicSqlSource(SqlNode... contents) throws IOException, SQLException {
293317
createBlogDataSource();
294318
final String resource = "org/apache/ibatis/builder/MapperConfig.xml";

0 commit comments

Comments
 (0)