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

@ -2,9 +2,10 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.30(2024-07-10)
# 5.8.30(2024-07-11)
### 🐣新特性
* 【core 】 Converter转换规则变更空对象、空值转为Bean时创建默认对象而非nullissue#3649@Github
### 🐞Bug修复
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题issue#IAAE88@Gitee

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

View File

@ -0,0 +1,17 @@
package cn.hutool.json;
import lombok.Data;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Issue3649Test {
@Test
public void toEmptyBeanTest() {
final Object bean = JSONUtil.toBean("{}", JSONConfig.create().setIgnoreError(true), EmptyBean.class);
assertEquals(new EmptyBean(), bean);
}
@Data
public static class EmptyBean {}
}