mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复xml转json再转bean失败问题
This commit is contained in:
parent
0c1b76c5f3
commit
8eab38eac8
CHANGELOG.md
hutool-core/src
main/java/cn/hutool/core
test/java/cn/hutool/core/util
hutool-json/src/test/java/cn/hutool/json
@ -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)
|
||||
|
@ -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)) {
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
52
hutool-json/src/test/java/cn/hutool/json/Issue3139Test.java
Executable file
52
hutool-json/src/test/java/cn/hutool/json/Issue3139Test.java
Executable file
@ -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 = "<r>\n" +
|
||||
" <c>\n" +
|
||||
" <s>1</s>\n" +
|
||||
" <p>str</p>\n" +
|
||||
" </c>\n" +
|
||||
"</r>";
|
||||
|
||||
final JSONObject jsonObject = XmlUtil.xmlToBean(XmlUtil.parseXml(xml).getDocumentElement(), JSONObject.class);
|
||||
final R bean = jsonObject.toBean(R.class);
|
||||
final List<C> 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> c;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user