Skip to content

fix: fix the problem that nested list cannot be parsed correctly #2136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
private String name;
private final String indexedName;
private String indexedName;
private String index;
private final String children;

Expand All @@ -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);
}
}

Expand Down
65 changes: 46 additions & 19 deletions src/test/java/org/apache/ibatis/binding/MapperMethodParamTest.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
Expand All @@ -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;
Expand Down Expand Up @@ -71,13 +73,38 @@ void parameterNameIsSizeUsingHashMap() {
}
}

/**
* Using nested lists as parameters
* <br/>
* Test whether {@link PropertyTokenizer#PropertyTokenizer(java.lang.String)} be parsed correctly
*/
@Test
void parameterNameIsSizeUsingNestedList() {
try (SqlSession session = sqlSessionFactory.openSession()) {
// Constructed parameters
List<List<List<Long>>> listOuter = new ArrayList<>();
List<List<Long>> listMiddle = new ArrayList<>();
List<Long> 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);

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

@Insert("insert into param_test (id, size) values(#{id}, #{list[0][0][0]})")
long insertSizeUsingNestedList(@Param("id") String id, @Param("list") List<List<List<Long>>> list);

@Select("select size from param_test where id = #{id}")
long selectSize(@Param("id") String id);
}
Expand Down