修复BeanUtil.copyProperties中mapToMap时key被转为String问题

This commit is contained in:
Looly 2024-07-06 11:36:22 +08:00
parent 1a46b2a39e
commit 48cc7f5b57
3 changed files with 41 additions and 8 deletions

View File

@ -9,6 +9,7 @@
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题issue#IAAE88@Gitee * 【core 】 修复因RFC3986理解有误导致的UrlPath处理冒号转义问题issue#IAAE88@Gitee
* 【core 】 修复FileUtil.cleanEmpty无法正确清空递归空目录问题pr#1233@Gitee * 【core 】 修复FileUtil.cleanEmpty无法正确清空递归空目录问题pr#1233@Gitee
* 【core 】 修复BeanUtil.copyProperties中mapToMap时key被转为String问题issue#3645@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.29(2024-07-03) # 5.8.29(2024-07-03)

View File

@ -42,18 +42,20 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
return; return;
} }
final String sKeyStr = copyOptions.editFieldName(sKey.toString()); if(sKey instanceof String){
// 对key做转换转换后为null的跳过 sKey = copyOptions.editFieldName((String) sKey);
if (null == sKeyStr) { // 对key做转换转换后为null的跳过
return; if (null == sKey) {
return;
}
} }
// 忽略不需要拷贝的 key, // 忽略不需要拷贝的 key,
if (false == copyOptions.testKeyFilter(sKeyStr)) { if (false == copyOptions.testKeyFilter(sKey)) {
return; return;
} }
final Object targetValue = target.get(sKeyStr); final Object targetValue = target.get(sKey);
// 非覆盖模式下如果目标值存在则跳过 // 非覆盖模式下如果目标值存在则跳过
if (false == copyOptions.override && null != targetValue) { if (false == copyOptions.override && null != targetValue) {
return; return;
@ -64,11 +66,11 @@ 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(sKeyStr, sValue); sValue = copyOptions.editFieldValue(sKey.toString(), sValue);
} }
// 目标赋值 // 目标赋值
target.put(sKeyStr, sValue); target.put(sKey, sValue);
}); });
return this.target; return this.target;
} }

View File

@ -0,0 +1,30 @@
package cn.hutool.core.bean;
import lombok.Data;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertNotNull;
public class Issue3645Test {
@Test
public void copyPropertiesTest() {
User p = new User();
p.setUserId(123L);
Map<Long, User> map = new HashMap<>();
map.put(123L,p);
Map<Long, User> m = new HashMap<>();
BeanUtil.copyProperties(map, m);
User u = m.get(123L);
assertNotNull(u);
}
@Data
static class User{
private Long userId;
}
}