修复CopyOptions.setFieldValueEditor后生成null值setIgnoreNullValue无效问题

This commit is contained in:
Looly
2024-08-22 12:05:24 +08:00
parent e1aa3f6ff5
commit 9be0fef520
6 changed files with 57 additions and 8 deletions

View File

@@ -73,9 +73,11 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
if(null != typeArguments && typeArguments.length > 1){
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
sValue = copyOptions.editFieldValue(sFieldName, sValue);
}
// 自定义值
sValue = copyOptions.editFieldValue(sFieldName, sValue);
// 目标赋值
if(null != sValue || false == copyOptions.ignoreNullValue){
//noinspection unchecked

View File

@@ -5,7 +5,6 @@ import cn.hutool.core.bean.PropDesc;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.CaseInsensitiveMap;
import cn.hutool.core.map.MapWrapper;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import java.lang.reflect.Type;
@@ -89,6 +88,7 @@ public class MapToBeanCopier<T> extends AbsCopier<Map<?, ?>, T> {
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
//Object newValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
Object newValue = this.copyOptions.convertField(fieldType, sValue);
// 自定义值
newValue = copyOptions.editFieldValue(sKeyStr, newValue);
// 目标赋值

View File

@@ -37,10 +37,6 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
if (null == sKey) {
return;
}
// 忽略空值
if (true == copyOptions.ignoreNullValue && sValue == null) {
return;
}
if(sKey instanceof String){
sKey = copyOptions.editFieldName((String) sKey);
@@ -66,7 +62,14 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
if (null != typeArguments) {
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
sValue = copyOptions.editFieldValue(sKey.toString(), sValue);
}
// 自定义值
sValue = copyOptions.editFieldValue(sKey.toString(), sValue);
// 忽略空值
if (true == copyOptions.ignoreNullValue && sValue == null) {
return;
}
// 目标赋值

View File

@@ -79,6 +79,8 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
if (false == copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
return;
}
// 自定义值
sValue = copyOptions.editFieldValue(tFieldName, sValue);
// 目标赋值

View File

@@ -0,0 +1,41 @@
package cn.hutool.core.bean;
import cn.hutool.core.bean.copier.CopyOptions;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
/**
* setFieldValueEditor编辑后的值理应继续判断ignoreNullValue
*/
public class Issue3702Test {
@Test
void mapToMapTest() {
Map<String,String> map= new HashMap<>();
map.put("a","");
map.put("b","b");
map.put("c","c");
map.put("d","d");
Map<String,String> map2= new HashMap<>();
map2.put("a","a1");
map2.put("b","b1");
map2.put("c","c1");
map2.put("d","d1");
CopyOptions option= CopyOptions.create()
.setIgnoreNullValue(true)
.setIgnoreError(true)
.setFieldValueEditor((name, value)->{
if(value.equals("")){
value=null;
}
return value;
});
BeanUtil.copyProperties(map,map2,option);
Assertions.assertEquals("{a=a1, b=b, c=c, d=d}", map2.toString());
System.out.println(map2);
}
}