This commit is contained in:
Looly 2024-02-29 11:20:29 +08:00
parent dbc1a24b07
commit df9e71a596
2 changed files with 39 additions and 2 deletions

View File

@ -79,7 +79,14 @@ public class CopyOptions implements Serializable {
protected boolean override = true;
/**
* 是否自动转换为驼峰方式
* 是否自动转换为驼峰方式<br>
* 此设置用于解决Bean和Map转换中的匹配问题而设置并不是一个强制参数
* <ol>
* <li>当map转bean时如果map中是下划线等非驼峰模式自动匹配对应的驼峰字段避免出现字段不拷贝问题</li>
* <li>当bean转bean时由于字段命名不规范使用了非驼峰方式增加兼容性</li>
* </ol>
* <p>
* 但是bean转Map和map转map时没有使用这个参数是因为没有匹配的必要转map不存在无法匹配到的问题因此此参数无效
*/
protected boolean autoTransCamelCase = true;
@ -320,7 +327,14 @@ public class CopyOptions implements Serializable {
/**
* 设置是否自动转换为驼峰方式<br>
* 一般用于map转bean和bean转bean出现非驼峰格式时在尝试转换失败的情况下是否二次检查转为驼峰匹配
* 一般用于map转bean和bean转bean出现非驼峰格式时在尝试转换失败的情况下是否二次检查转为驼峰匹配<br>
* 此设置用于解决Bean和Map转换中的匹配问题而设置并不是一个强制参数
* <ol>
* <li>当map转bean时如果map中是下划线等非驼峰模式自动匹配对应的驼峰字段避免出现字段不拷贝问题</li>
* <li>当bean转bean时由于字段命名不规范使用了非驼峰方式增加兼容性</li>
* </ol>
* <p>
* 但是bean转Map和map转map时没有使用这个参数是因为没有匹配的必要转map不存在无法匹配到的问题因此此参数无效
*
* @param autoTransCamelCase 是否自动转换为驼峰方式
* @return this

View File

@ -0,0 +1,23 @@
package org.dromara.hutool.core.bean;
import org.dromara.hutool.core.bean.copier.CopyOptions;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Map;
public class Issue3497Test {
@Test
public void setFieldEditorTest() {
final Map<String, String> aB = MapUtil.builder("a_b", "1").build();
final Map<?, ?> bean = BeanUtil.toBean(aB, Map.class, CopyOptions.of().setFieldEditor((entry)->{
entry.setKey(StrUtil.toCamelCase(entry.getKey()));
return entry;
}));
Assertions.assertEquals(bean.toString(), "{aB=1}");
}
}