mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复CopyOptions.setFieldValueEditor后生成null值setIgnoreNullValue无效问题
This commit is contained in:
parent
e1aa3f6ff5
commit
9be0fef520
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.32(2024-08-15)
|
# 5.8.32(2024-08-22)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
|
||||||
@ -10,6 +10,7 @@
|
|||||||
* 【http 】 修复getFileNameFromDisposition不符合规范问题(issue#IAKBPD@Gitee)
|
* 【http 】 修复getFileNameFromDisposition不符合规范问题(issue#IAKBPD@Gitee)
|
||||||
* 【crypto 】 修复SymmetricCrypto.setParams和setRandom没有加锁问题(issue#IAJIY3@Gitee)
|
* 【crypto 】 修复SymmetricCrypto.setParams和setRandom没有加锁问题(issue#IAJIY3@Gitee)
|
||||||
* 【crypto 】 修复ZipUtil压缩成流的方法检查文件时报错问题(issue#3697@Github)
|
* 【crypto 】 修复ZipUtil压缩成流的方法检查文件时报错问题(issue#3697@Github)
|
||||||
|
* 【core 】 修复CopyOptions.setFieldValueEditor后生成null值setIgnoreNullValue无效问题(issue#3702@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.31(2024-08-12)
|
# 5.8.31(2024-08-12)
|
||||||
|
@ -73,9 +73,11 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
|
|||||||
if(null != typeArguments && typeArguments.length > 1){
|
if(null != typeArguments && typeArguments.length > 1){
|
||||||
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
||||||
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
|
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
|
||||||
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自定义值
|
||||||
|
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
if(null != sValue || false == copyOptions.ignoreNullValue){
|
if(null != sValue || false == copyOptions.ignoreNullValue){
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
|
@ -5,7 +5,6 @@ import cn.hutool.core.bean.PropDesc;
|
|||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
import cn.hutool.core.map.MapWrapper;
|
import cn.hutool.core.map.MapWrapper;
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
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());
|
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
|
||||||
//Object newValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
//Object newValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
||||||
Object newValue = this.copyOptions.convertField(fieldType, sValue);
|
Object newValue = this.copyOptions.convertField(fieldType, sValue);
|
||||||
|
// 自定义值
|
||||||
newValue = copyOptions.editFieldValue(sKeyStr, newValue);
|
newValue = copyOptions.editFieldValue(sKeyStr, newValue);
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
|
@ -37,10 +37,6 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
|||||||
if (null == sKey) {
|
if (null == sKey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 忽略空值
|
|
||||||
if (true == copyOptions.ignoreNullValue && sValue == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(sKey instanceof String){
|
if(sKey instanceof String){
|
||||||
sKey = copyOptions.editFieldName((String) sKey);
|
sKey = copyOptions.editFieldName((String) sKey);
|
||||||
@ -66,7 +62,14 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
|||||||
if (null != typeArguments) {
|
if (null != typeArguments) {
|
||||||
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
||||||
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
|
@ -79,6 +79,8 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
|
|||||||
if (false == copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
|
if (false == copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自定义值
|
||||||
sValue = copyOptions.editFieldValue(tFieldName, sValue);
|
sValue = copyOptions.editFieldValue(tFieldName, sValue);
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user