Skip to content

Commit eeffd67

Browse files
committed
fix: fix the problem that nested list cannot be parsed correctly
close #2103
1 parent ac2f383 commit eeffd67

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

src/main/java/org/apache/ibatis/reflection/property/PropertyTokenizer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
2424
private String name;
25-
private final String indexedName;
25+
private String indexedName;
2626
private String index;
2727
private final String children;
2828

@@ -35,11 +35,13 @@ public PropertyTokenizer(String fullname) {
3535
name = fullname;
3636
children = null;
3737
}
38+
3839
indexedName = name;
39-
delim = name.indexOf('[');
40+
delim = name.lastIndexOf('[');
4041
if (delim > -1) {
4142
index = name.substring(delim + 1, name.length() - 1);
4243
name = name.substring(0, delim);
44+
indexedName = fullname.substring(0, delim);
4345
}
4446
}
4547

src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
/**
2-
* Copyright 2009-2020 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.
2+
* Copyright 2009-2020 the original author or authors.
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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.
1515
*/
1616
package org.apache.ibatis.binding;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
20-
import java.util.HashMap;
21-
22-
import javax.sql.DataSource;
23-
2418
import org.apache.ibatis.BaseDataTest;
2519
import org.apache.ibatis.annotations.Insert;
2620
import org.apache.ibatis.annotations.Param;
2721
import org.apache.ibatis.annotations.Select;
2822
import org.apache.ibatis.mapping.Environment;
23+
import org.apache.ibatis.reflection.property.PropertyTokenizer;
2924
import org.apache.ibatis.session.Configuration;
3025
import org.apache.ibatis.session.SqlSession;
3126
import org.apache.ibatis.session.SqlSessionFactory;
@@ -35,6 +30,13 @@
3530
import org.junit.jupiter.api.BeforeAll;
3631
import org.junit.jupiter.api.Test;
3732

33+
import javax.sql.DataSource;
34+
import java.util.ArrayList;
35+
import java.util.HashMap;
36+
import java.util.List;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
3840
class MapperMethodParamTest {
3941

4042
private static SqlSessionFactory sqlSessionFactory;
@@ -71,13 +73,38 @@ void parameterNameIsSizeUsingHashMap() {
7173
}
7274
}
7375

76+
/**
77+
* Using nested lists as parameters
78+
* <br/>
79+
* Test whether {@link PropertyTokenizer#PropertyTokenizer(java.lang.String)} be parsed correctly
80+
*/
81+
@Test
82+
void parameterNameIsSizeUsingNestedList() {
83+
try (SqlSession session = sqlSessionFactory.openSession()) {
84+
// Constructed parameters
85+
List<List<List<Long>>> listOuter = new ArrayList<>();
86+
List<List<Long>> listMiddle = new ArrayList<>();
87+
List<Long> listInner = new ArrayList<>();
88+
listOuter.add(listMiddle);
89+
listMiddle.add(listInner);
90+
listInner.add(Long.MAX_VALUE);
91+
92+
Mapper mapper = session.getMapper(Mapper.class);
93+
mapper.insertSizeUsingNestedList("foo", listOuter);
94+
assertThat(mapper.selectSize("foo")).isEqualTo(Long.MAX_VALUE);
95+
}
96+
}
97+
7498
interface Mapper {
7599
@Insert("insert into param_test (id, size) values(#{id}, #{size})")
76100
void insert(@Param("id") String id, @Param("size") long size);
77101

78102
@Insert("insert into param_test (id, size) values(#{id}, #{size})")
79103
void insertUsingHashMap(HashMap<String, Object> params);
80104

105+
@Insert("insert into param_test (id, size) values(#{id}, #{list[0][0][0]})")
106+
long insertSizeUsingNestedList(@Param("id") String id, @Param("list") List<List<List<Long>>> list);
107+
81108
@Select("select size from param_test where id = #{id}")
82109
long selectSize(@Param("id") String id);
83110
}

0 commit comments

Comments
 (0)