diff --git a/CHANGELOG.md b/CHANGELOG.md index 9175381b4..84592c688 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.20(2023-06-12) +# 5.8.20(2023-06-14) ### 🐣新特性 * 【core 】 UrlQuery增加setStrict方法,区分是否严格模式(issue#I78PB1@Gitee) @@ -17,6 +17,7 @@ * 【core 】 修复Table#contains空指针问题(issue#3135@Gitee) * 【core 】 修复FileUtil.checkSlip方法缺陷(issue#3140@Github) * 【extra 】 修复Sftp中exists方法父目录不存在时报错(issue#I7CSQ9@Gitee) +* 【extra 】 修复xml转json再转bean失败问题(issue#3139@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.19(2023-05-27) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index a5cbfa0fa..9d52ab3c3 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -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)) { diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 9750fc445..ab9b68894 100755 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -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 * diff --git a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java index 294897097..34eb76b84 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/XmlUtilTest.java @@ -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 = "\n" + + " \n" + + " 1\n" + + "

str

\n" + + "
\n" + + "
"; + + 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; + } } diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue3139Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue3139Test.java new file mode 100755 index 000000000..b5fd4a932 --- /dev/null +++ b/hutool-json/src/test/java/cn/hutool/json/Issue3139Test.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.json; + +import cn.hutool.core.lang.Console; +import cn.hutool.core.util.XmlUtil; +import lombok.Data; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class Issue3139Test { + + @Test + public void toBeanTest() { + final String xml = "\n" + + " \n" + + " 1\n" + + "

str

\n" + + "
\n" + + "
"; + + final JSONObject jsonObject = XmlUtil.xmlToBean(XmlUtil.parseXml(xml).getDocumentElement(), JSONObject.class); + final R bean = jsonObject.toBean(R.class); + final List c = bean.getC(); + Assert.assertEquals(1, c.size()); + Assert.assertEquals("1", c.get(0).getS()); + Assert.assertEquals("str", c.get(0).getP()); + } + + @Data + static class C { + String s; + String p; + } + + @Data + static class R { + List c; + } +}