Skip to content

Commit 656cb58

Browse files
committed
BoundSql#hasAdditionalParameter should check only the existence of the parent parameter. This should yield more accurate error message in cases like #411
1 parent 01e1453 commit 656cb58

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/main/java/org/apache/ibatis/mapping/BoundSql.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121

2222
import org.apache.ibatis.reflection.MetaObject;
23+
import org.apache.ibatis.reflection.property.PropertyTokenizer;
2324
import org.apache.ibatis.session.Configuration;
2425

2526
/**
@@ -62,7 +63,9 @@ public Object getParameterObject() {
6263
}
6364

6465
public boolean hasAdditionalParameter(String name) {
65-
return metaParameters.hasGetter(name);
66+
PropertyTokenizer prop = new PropertyTokenizer(name);
67+
String indexedName = prop.getIndexedName();
68+
return additionalParameters.containsKey(indexedName);
6669
}
6770

6871
public void setAdditionalParameter(String name, Object value) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
package org.apache.ibatis.mapping;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.ibatis.session.Configuration;
26+
import org.junit.Test;
27+
28+
public class BoundSqlTest {
29+
30+
@Test
31+
public void testHasAdditionalParameter() throws Exception {
32+
List<ParameterMapping> params = Collections.emptyList();
33+
BoundSql boundSql = new BoundSql(new Configuration(), "some sql", params, new Object());
34+
35+
Map<String, String> map = new HashMap<String, String>();
36+
map.put("key1", "value1");
37+
boundSql.setAdditionalParameter("map", map);
38+
39+
Person bean = new Person();
40+
bean.id = 1;
41+
boundSql.setAdditionalParameter("person", bean);
42+
43+
assertFalse(boundSql.hasAdditionalParameter("pet"));
44+
assertFalse(boundSql.hasAdditionalParameter("pet.name"));
45+
46+
assertTrue(boundSql.hasAdditionalParameter("map"));
47+
assertTrue(boundSql.hasAdditionalParameter("map.key1"));
48+
assertTrue("should return true even if the child property does not exists.", boundSql.hasAdditionalParameter("map.key2"));
49+
50+
assertTrue(boundSql.hasAdditionalParameter("person"));
51+
assertTrue(boundSql.hasAdditionalParameter("person.id"));
52+
assertTrue("should return true even if the child property does not exists.", boundSql.hasAdditionalParameter("person.name"));
53+
}
54+
55+
public static class Person {
56+
public Integer id;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)