Converter转换规则变更,空对象、空值转为Bean时,创建默认对象,而非nul

This commit is contained in:
Looly
2024-07-11 12:43:40 +08:00
parent 2b995a5370
commit b2510cf661
4 changed files with 28 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package cn.hutool.core.convert;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.impl.*;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.lang.TypeReference;
@@ -340,6 +341,12 @@ public class ConverterRegistry implements Serializable {
return (T) converter.convert(value, (Class<?>) defaultValue);
}
// 空值转空Bean
if(ObjectUtil.isEmpty(value)){
// issue#3649 空值转空对象,则直接实例化
return (T) ReflectUtil.newInstanceIfPossible(rowType);
}
// 表示非需要特殊转换的对象
return null;
}

View File

@@ -33,7 +33,8 @@ public class Issue3136Test {
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);
Assert.assertEquals("02", smsRes.getCode());
Assert.assertNull(smsRes.getMessage());
Assert.assertNotNull(smsRes.getMessage());
Assert.assertEquals(new Message(), smsRes.getMessage());
}
@Data