This commit is contained in:
Looly 2023-12-06 17:26:51 +08:00
parent a9cc50e98e
commit c529fa129f

View File

@ -13,6 +13,7 @@
package org.dromara.hutool.core.bean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.dromara.hutool.core.bean.copier.CopyOptions;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.map.MapUtil;
@ -33,16 +34,33 @@ public class IssueI8M38TTest {
final ArrayList<SubPerson> subPeople = ListUtil.of(subPerson);
final HashMap<String, String> mapping = MapUtil.of("subName", "name");
final List<Person> people = BeanUtil.copyToList(subPeople, Person.class,
// 不覆盖模式下当第一次subName已经拷贝到值后续不再覆盖
CopyOptions.of().setFieldMapping(mapping).setOverride(false));
Assertions.assertEquals(subPerson.getSubName(), people.get(0).getName());
}
@Test
void copyListByMappingTest2() {
final SubPerson subPerson = new SubPerson();
subPerson.setAge(14);
subPerson.setOpenid(112233);
subPerson.setSubName("sub名字");
final ArrayList<SubPerson> subPeople = ListUtil.of(subPerson);
final HashMap<String, String> mapping = MapUtil.of("subName", "name");
// subName复制到name后name字段继续拷贝覆盖了subName的值
mapping.put("name", "aaa");
final List<Person> people = BeanUtil.copyToList(subPeople, Person.class,
CopyOptions.of().setFieldMapping(mapping));
Assertions.assertEquals(subPerson.getSubName(), people.get(0).getName());
}
@EqualsAndHashCode(callSuper = true)
@Data
static class SubPerson {
private int age;
private int openid;
static class SubPerson extends Person{
private String subName;
}