mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
f88e7ab2f3
commit
87b88b395e
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.7.16 (2021-10-24)
|
||||
# 5.7.16 (2021-10-26)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 增加DateTime.toLocalDateTime
|
||||
@ -13,6 +13,7 @@
|
||||
* 【core 】 NumberUtil.compare修正注释说明(issue#I4FAJ1@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复UrlBuilder.addPath歧义问题(issue#1912@Github)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -322,13 +322,25 @@ public final class UrlBuilder implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加路径节点
|
||||
* 增加路径,在现有路径基础上追加路径
|
||||
*
|
||||
* @param path 路径,例如aaa/bbb/ccc
|
||||
* @return this
|
||||
*/
|
||||
public UrlBuilder addPath(CharSequence path) {
|
||||
UrlPath.of(path, this.charset).getSegments().forEach(this::addPathSegment);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加路径节点,路径节点中的"/"会被转义为"%2F"
|
||||
*
|
||||
* @param segment 路径节点
|
||||
* @return this
|
||||
* @since 5.7.16
|
||||
*/
|
||||
public UrlBuilder addPath(String segment) {
|
||||
if (StrUtil.isBlank(segment)) {
|
||||
public UrlBuilder addPathSegment(CharSequence segment) {
|
||||
if (StrUtil.isEmpty(segment)) {
|
||||
return this;
|
||||
}
|
||||
if (null == this.path) {
|
||||
@ -341,19 +353,13 @@ public final class UrlBuilder implements Serializable {
|
||||
/**
|
||||
* 追加path节点
|
||||
*
|
||||
* @param segment path节点
|
||||
* @param path path节点
|
||||
* @return this
|
||||
* @deprecated 方法重复,请使用{@link #addPath(CharSequence)}
|
||||
*/
|
||||
public UrlBuilder appendPath(CharSequence segment) {
|
||||
if (StrUtil.isEmpty(segment)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (this.path == null) {
|
||||
this.path = new UrlPath();
|
||||
}
|
||||
this.path.add(segment);
|
||||
return this;
|
||||
@Deprecated
|
||||
public UrlBuilder appendPath(CharSequence path) {
|
||||
return addPath(path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,7 +29,7 @@ public class UrlPath {
|
||||
* @param charset decode用的编码,null表示不做decode
|
||||
* @return UrlPath
|
||||
*/
|
||||
public static UrlPath of(String pathStr, Charset charset) {
|
||||
public static UrlPath of(CharSequence pathStr, Charset charset) {
|
||||
final UrlPath urlPath = new UrlPath();
|
||||
urlPath.parse(pathStr, charset);
|
||||
return urlPath;
|
||||
@ -97,7 +97,7 @@ public class UrlPath {
|
||||
* @param charset decode编码,null表示不解码
|
||||
* @return this
|
||||
*/
|
||||
public UrlPath parse(String path, Charset charset) {
|
||||
public UrlPath parse(CharSequence path, Charset charset) {
|
||||
if (StrUtil.isNotEmpty(path)) {
|
||||
// 原URL中以/结尾,则这个规则需保留,issue#I1G44J@Gitee
|
||||
if(StrUtil.endWith(path, CharUtil.SLASH)){
|
||||
|
@ -281,4 +281,29 @@ public class UrlBuilderTest {
|
||||
final UrlBuilder urlBuilder = UrlBuilder.ofHttp(url);
|
||||
Assert.assertEquals(url, urlBuilder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addPathEncodeTest(){
|
||||
String url = UrlBuilder.create()
|
||||
.setScheme("https")
|
||||
.setHost("domain.cn")
|
||||
.addPath("api")
|
||||
.addPath("xxx")
|
||||
.addPath("bbb")
|
||||
.build();
|
||||
|
||||
Assert.assertEquals("https://domain.cn/api/xxx/bbb", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addPathEncodeTest2(){
|
||||
// https://github.com/dromara/hutool/issues/1912
|
||||
String url = UrlBuilder.create()
|
||||
.setScheme("https")
|
||||
.setHost("domain.cn")
|
||||
.addPath("/api/xxx/bbb")
|
||||
.build();
|
||||
|
||||
Assert.assertEquals("https://domain.cn/api/xxx/bbb", url);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user