mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复MapToMap中ignoreNullValue无效问题
This commit is contained in:
parent
ff4496e679
commit
dca6cbe4b1
@ -19,6 +19,7 @@
|
||||
* 【core 】 修复ReUtil.replaceAll替换变量错误问题(pr#2639@Github)
|
||||
* 【core 】 修复FileNameUtil.mainName二级扩展名获取错误问题(issue#2642@Github)
|
||||
* 【cache 】 修复LRUCache移除事件监听失效问题(issue#2647@Github)
|
||||
* 【core 】 修复MapToMap中ignoreNullValue无效问题(issue#2647@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -37,6 +37,11 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
||||
if (null == sKey) {
|
||||
return;
|
||||
}
|
||||
// 忽略空值
|
||||
if (true == copyOptions.ignoreNullValue && sValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String sKeyStr = copyOptions.editFieldName(sKey.toString());
|
||||
// 对key做转换,转换后为null的跳过
|
||||
if (null == sKeyStr) {
|
||||
@ -48,10 +53,6 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
||||
if (false == copyOptions.override && null != targetValue) {
|
||||
return;
|
||||
}
|
||||
// 忽略空值
|
||||
if (true == copyOptions.ignoreNullValue && sValue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取目标值真实类型并转换源值
|
||||
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
|
||||
|
@ -422,6 +422,21 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals("sub测试", map.get("subName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyPropertiesMapToMapIgnoreNullTest() {
|
||||
// 测试MapToMap
|
||||
final Map<String, Object> p1 = new HashMap<>();
|
||||
p1.put("isSlow", true);
|
||||
p1.put("name", "测试");
|
||||
p1.put("subName", null);
|
||||
|
||||
final Map<String, Object> map = MapUtil.newHashMap();
|
||||
BeanUtil.copyProperties(p1, map, CopyOptions.create().setIgnoreNullValue(true));
|
||||
Assert.assertTrue((Boolean) map.get("isSlow"));
|
||||
Assert.assertEquals("测试", map.get("name"));
|
||||
Assert.assertFalse(map.containsKey("subName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trimBeanStrFieldsTest() {
|
||||
final Person person = new Person();
|
||||
|
@ -10,7 +10,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Map转换单元测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@ -18,23 +18,23 @@ public class MapConvertTest {
|
||||
|
||||
@Test
|
||||
public void beanToMapTest() {
|
||||
User user = new User();
|
||||
final User user = new User();
|
||||
user.setName("AAA");
|
||||
user.setAge(45);
|
||||
|
||||
HashMap<?, ?> map = Convert.convert(HashMap.class, user);
|
||||
final HashMap<?, ?> map = Convert.convert(HashMap.class, user);
|
||||
Assert.assertEquals("AAA", map.get("name"));
|
||||
Assert.assertEquals(45, map.get("age"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToMapTest() {
|
||||
Map<String, Object> srcMap = MapBuilder
|
||||
final Map<String, Object> srcMap = MapBuilder
|
||||
.create(new HashMap<String, Object>())
|
||||
.put("name", "AAA")
|
||||
.put("age", 45).map();
|
||||
|
||||
LinkedHashMap<?, ?> map = Convert.convert(LinkedHashMap.class, srcMap);
|
||||
final LinkedHashMap<?, ?> map = Convert.convert(LinkedHashMap.class, srcMap);
|
||||
Assert.assertEquals("AAA", map.get("name"));
|
||||
Assert.assertEquals(45, map.get("age"));
|
||||
}
|
||||
@ -47,7 +47,7 @@ public class MapConvertTest {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ public class MapConvertTest {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
public void setAge(final int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user