修复HttpUtil.normalizeParams规则问题(issue#IBQIYQ@Gitee)

This commit is contained in:
Looly 2025-03-07 09:26:44 +08:00
parent 2046809264
commit 8d34a4d26e
3 changed files with 30 additions and 13 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.37(2025-03-03)
# 5.8.37(2025-03-07)
### 🐣新特性
* 【json 】 ObjectMapper删除重复trimpr#3859@Github
@ -16,6 +16,7 @@
* 【setting】 修复`SettingLoader`load未抛出异常导致配置文件无法正常遍历的问题pr#3868@Github
* 【cache 】 修复`ReentrantCache#getOrRemoveExpired`方法丢失onRemove触发问题pr#1315@Gitee
* 【json 】 修复`JsonUtil.toBean`泛型数组类型丢失问题pr#3876@Github
* 【http 】 修复`HttpUtil.normalizeParams`规则问题issue#IBQIYQ@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.8.36(2025-02-18)

View File

@ -9,11 +9,7 @@ import cn.hutool.core.map.MapUtil;
import cn.hutool.core.net.RFC3986;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.*;
import cn.hutool.http.cookie.GlobalCookieManager;
import cn.hutool.http.server.SimpleServer;
@ -557,17 +553,17 @@ public class HttpUtil {
pos = i + 1;
}
} else if (c == '&') { // 参数对的分界点
if (pos != i) {
if (null == name) {
// 对于像&a&这类无参数值的字符串我们将name为a的值设为""
if (null == name) {
// 对于像&a&这类无参数值的字符串我们将name为a的值设为""
if(pos != i){
name = paramPart.substring(pos, i);
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=');
} else {
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=')
.append(RFC3986.QUERY_PARAM_VALUE.encode(paramPart.substring(pos, i), charset)).append('&');
}
name = null;
} else {
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=')
.append(RFC3986.QUERY_PARAM_VALUE.encode(paramPart.substring(pos, i), charset)).append('&');
}
name = null;
pos = i + 1;
}
}

View File

@ -0,0 +1,20 @@
package cn.hutool.http;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
public class IssueIBQIYQTest {
@Test
void normalizeParamsTest1() {
Map<String, Object> map = new HashMap<>();
map.put("id", "");
map.put("type", "4");
String url = HttpUtil.toParams(map);
Assertions.assertEquals("id=&type=4", url);
url = HttpUtil.normalizeParams(url, null);
Assertions.assertEquals("id=&type=4", url);
}
}