修复xml转json再转bean失败问题

This commit is contained in:
Looly
2023-06-14 13:33:27 +08:00
parent 0c1b76c5f3
commit 8eab38eac8
5 changed files with 100 additions and 3 deletions

View File

@@ -2175,7 +2175,13 @@ public class CollUtil {
if (value instanceof Iterator) {
iter = (Iterator) value;
} else if (value instanceof Iterable) {
iter = ((Iterable) value).iterator();
if(value instanceof Map && BeanUtil.isBean(TypeUtil.getClass(elementType))){
//https://github.com/dromara/hutool/issues/3139
// 如果值为Map而目标为一个Bean则Map应整体转换为Bean而非拆分成Entry转换
iter = new ArrayIter<>(new Object[]{value});
}else{
iter = ((Iterable) value).iterator();
}
} else if (value instanceof Enumeration) {
iter = new EnumerationIter<>((Enumeration) value);
} else if (ArrayUtil.isArray(value)) {

View File

@@ -3459,13 +3459,25 @@ public class FileUtil extends PathUtil {
*/
public static File checkSlip(File parentFile, File file) throws IllegalArgumentException {
if (null != parentFile && null != file) {
if(!file.toPath().startsWith(parentFile.toPath())){
if (false == startsWith(parentFile, file)) {
throw new IllegalArgumentException("New file is outside of the parent dir: " + file.getName());
}
}
return file;
}
/**
* 检查父文件是否为文件真正的父目录
*
* @param parentFile 父目录
* @param file 文件
* @return 是否为文件真正的父目录
*/
public static boolean startsWith(final File parentFile, final File file) {
return PathUtil.toAbsNormal(parentFile.toPath())
.startsWith(PathUtil.toAbsNormal(file.toPath()));
}
/**
* 根据文件扩展名获得MimeType
*

View File

@@ -18,6 +18,7 @@ import org.xml.sax.helpers.DefaultHandler;
import javax.xml.xpath.XPathConstants;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -350,4 +351,29 @@ public class XmlUtilTest {
private String age;
private String email;
}
@Test
public void issue3139Test() {
final String xml = "<r>\n" +
" <c>\n" +
" <s>1</s>\n" +
" <p>str</p>\n" +
" </c>\n" +
"</r>";
final R r = XmlUtil.xmlToBean(XmlUtil.parseXml(xml), R.class);
Assert.assertEquals("1", r.getC().get(0).getS());
Assert.assertEquals("str", r.getC().get(0).getP());
}
@Data
static class C {
String s;
String p;
}
@Data
static class R {
List<C> c;
}
}