Skip to content

Commit

Permalink
修复XmlUtil.xmlToBean空节点转换失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jun 16, 2023
1 parent d53ac71 commit 7351aec
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* 【extra 】 修复Sftp中exists方法父目录不存在时报错(issue#I7CSQ9@Gitee)
* 【extra 】 修复xml转json再转bean失败问题(issue#3139@Github)
* 【poi 】 修复RowUtil传入参数错误问题(issue#3139@Github)
* 【poi 】 修复XmlUtil.xmlToBean空节点转换失败问题(issue#3136@Github)

-------------------------------------------------------------------------------------------------------------
# 5.8.19(2023-05-27)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -79,6 +80,9 @@ protected T convertInternal(Object value) {
} else if(value instanceof byte[]){
// 尝试反序列化
return ObjectUtil.deserialize((byte[])value);
} else if(StrUtil.isEmptyIfStr(value)){
// issue#3136
return null;
}

throw new ConvertException("Unsupported source type: {}", value.getClass());
Expand Down
67 changes: 67 additions & 0 deletions hutool-core/src/test/java/cn/hutool/core/util/Issue3136Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 looly([email protected])
* 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.core.util;

import lombok.Data;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class Issue3136Test {

@Test
public void xmlToBeanTest() {
final String xmlStr = "<?xml version=\"1.0\" encoding=\"gbk\" ?><response><code>02</code><message></message></response>";
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);

Assert.assertEquals("02", smsRes.getCode());
Assert.assertNull(smsRes.getMessage());
}

@Data
static class SmsRes {
/**
* 状态码.
*/
private String code;

/**
* 消息.
*/
private Message message;
}

@Data
static class Message {

/**
* 消息项.
*/
private List<MessageItem> item = new ArrayList<>();
}

@Data
static class MessageItem {

/**
* 手机号.
*/
private String desmobile;
/**
* 消息id.
*/
private String msgid;
}
}

0 comments on commit 7351aec

Please sign in to comment.