diff --git a/src/main/java/org/apache/ibatis/reflection/property/PropertyTokenizer.java b/src/main/java/org/apache/ibatis/reflection/property/PropertyTokenizer.java index 2c5f752eed0..81f48194abe 100644 --- a/src/main/java/org/apache/ibatis/reflection/property/PropertyTokenizer.java +++ b/src/main/java/org/apache/ibatis/reflection/property/PropertyTokenizer.java @@ -22,7 +22,7 @@ */ public class PropertyTokenizer implements Iterator { private String name; - private final String indexedName; + private String indexedName; private String index; private final String children; @@ -31,15 +31,19 @@ public PropertyTokenizer(String fullname) { if (delim > -1) { name = fullname.substring(0, delim); children = fullname.substring(delim + 1); + indexedName = name; + return; } else { name = fullname; children = null; } + indexedName = name; - delim = name.indexOf('['); + delim = name.lastIndexOf('['); if (delim > -1) { index = name.substring(delim + 1, name.length() - 1); name = name.substring(0, delim); + indexedName = fullname.substring(0, delim); } } diff --git a/src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java b/src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java index 11f3332aa8b..88b1c86e828 100644 --- a/src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java +++ b/src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java @@ -1,31 +1,26 @@ /** - * Copyright 2009-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2009-2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.apache.ibatis.binding; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.HashMap; - -import javax.sql.DataSource; - import org.apache.ibatis.BaseDataTest; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.reflection.property.PropertyTokenizer; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -35,6 +30,13 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import javax.sql.DataSource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + class MapperMethodParamTest { private static SqlSessionFactory sqlSessionFactory; @@ -71,6 +73,28 @@ void parameterNameIsSizeUsingHashMap() { } } + /** + * Using nested lists as parameters + *
+ * Test whether {@link PropertyTokenizer#PropertyTokenizer(java.lang.String)} be parsed correctly + */ + @Test + void parameterNameIsSizeUsingNestedList() { + try (SqlSession session = sqlSessionFactory.openSession()) { + // Constructed parameters + List>> listOuter = new ArrayList<>(); + List> listMiddle = new ArrayList<>(); + List listInner = new ArrayList<>(); + listOuter.add(listMiddle); + listMiddle.add(listInner); + listInner.add(Long.MAX_VALUE); + + Mapper mapper = session.getMapper(Mapper.class); + mapper.insertSizeUsingNestedList("foo", listOuter); + assertThat(mapper.selectSize("foo")).isEqualTo(Long.MAX_VALUE); + } + } + interface Mapper { @Insert("insert into param_test (id, size) values(#{id}, #{size})") void insert(@Param("id") String id, @Param("size") long size); @@ -78,6 +102,9 @@ interface Mapper { @Insert("insert into param_test (id, size) values(#{id}, #{size})") void insertUsingHashMap(HashMap params); + @Insert("insert into param_test (id, size) values(#{id}, #{list[0][0][0]})") + long insertSizeUsingNestedList(@Param("id") String id, @Param("list") List>> list); + @Select("select size from param_test where id = #{id}") long selectSize(@Param("id") String id); }