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