mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Converter转换规则变更,空对象、空值转为Bean时,创建默认对象,而非nul
This commit is contained in:
parent
2b995a5370
commit
b2510cf661
@ -2,9 +2,10 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.30(2024-07-10)
|
# 5.8.30(2024-07-11)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【core 】 Converter转换规则变更,空对象、空值转为Bean时,创建默认对象,而非null(issue#3649@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题(issue#IAAE88@Gitee)
|
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题(issue#IAAE88@Gitee)
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.core.convert;
|
|||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.convert.impl.*;
|
import cn.hutool.core.convert.impl.*;
|
||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.lang.Opt;
|
import cn.hutool.core.lang.Opt;
|
||||||
import cn.hutool.core.lang.Pair;
|
import cn.hutool.core.lang.Pair;
|
||||||
import cn.hutool.core.lang.TypeReference;
|
import cn.hutool.core.lang.TypeReference;
|
||||||
@ -340,6 +341,12 @@ public class ConverterRegistry implements Serializable {
|
|||||||
return (T) converter.convert(value, (Class<?>) defaultValue);
|
return (T) converter.convert(value, (Class<?>) defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 空值转空Bean
|
||||||
|
if(ObjectUtil.isEmpty(value)){
|
||||||
|
// issue#3649 空值转空对象,则直接实例化
|
||||||
|
return (T) ReflectUtil.newInstanceIfPossible(rowType);
|
||||||
|
}
|
||||||
|
|
||||||
// 表示非需要特殊转换的对象
|
// 表示非需要特殊转换的对象
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,8 @@ public class Issue3136Test {
|
|||||||
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);
|
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);
|
||||||
|
|
||||||
Assert.assertEquals("02", smsRes.getCode());
|
Assert.assertEquals("02", smsRes.getCode());
|
||||||
Assert.assertNull(smsRes.getMessage());
|
Assert.assertNotNull(smsRes.getMessage());
|
||||||
|
Assert.assertEquals(new Message(), smsRes.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
17
hutool-json/src/test/java/cn/hutool/json/Issue3649Test.java
Normal file
17
hutool-json/src/test/java/cn/hutool/json/Issue3649Test.java
Normal 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 {}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user