Skip to content

Commit 1d9cf7b

Browse files
committed
fix: encode url contain special char
1 parent 987229a commit 1d9cf7b

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

client/src/main/java/cn/vika/client/api/model/ApiQueryParam.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private String formatSort(String fieldName, Order order) {
128128
sortMap.put("field", fieldName);
129129
sortMap.put("order", order.name().toLowerCase());
130130
try {
131-
return new ObjectMapper().writeValueAsString(sortMap);
131+
return UrlEncoder.encodeURIComponent(new ObjectMapper().writeValueAsString(sortMap));
132132
}
133133
catch (JsonProcessingException e) {
134134
throw new IllegalArgumentException("can't format sort parameter", e);

client/src/test/java/cn/vika/client/api/model/ApiQueryParamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testParamWithSort() {
3737
ApiQueryParam queryParam = new ApiQueryParam()
3838
.withSort("id", Order.DESC);
3939
Map<String, String> queryMap = queryParam.toMap();
40-
assertThat(queryMap).containsOnlyKeys("sort[]");
40+
assertThat(queryMap).containsOnlyKeys("sort%5B%5D");
4141
}
4242

4343
@Test
@@ -46,7 +46,7 @@ public void testParamWithSortMulti() {
4646
.withSort("id", Order.DESC)
4747
.withSort("name", Order.DESC);
4848
Map<String, String> queryMap = queryParam.toMap();
49-
assertThat(queryMap).containsOnlyKeys("sort[].0", "sort[].1");
49+
assertThat(queryMap).containsOnlyKeys("sort%5B%5D.0", "sort%5B%5D.1");
5050
}
5151

5252
@Test
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cn.vika.core.utils;
22

3+
import java.util.LinkedHashMap;
34
import java.util.Map;
45

5-
import org.assertj.core.util.Maps;
66
import org.junit.jupiter.api.Test;
77

8+
import static cn.vika.core.utils.UrlEncoder.encodeURIComponent;
89
import static org.assertj.core.api.Assertions.assertThat;
910

1011
/**
@@ -14,13 +15,14 @@ public class MapUtilTest {
1415

1516
@Test
1617
public void testExtractKeyToVariables() {
17-
Map<String, String> uriVariables = Maps.newHashMap("page", "1");
18+
Map<String, String> uriVariables = new LinkedHashMap<>(6);
19+
uriVariables.put("page", "1");
1820
uriVariables.put("pageSize", "10");
19-
uriVariables.put("sort[].0", "{\"field\":\"id\",\"order\":\"desc\"}");
20-
uriVariables.put("sort[].1", "{\"field\":\"name\",\"order\":\"desc\"}");
21+
uriVariables.put(encodeURIComponent("sort[].0"), encodeURIComponent("{\"field\":\"id\",\"order\":\"desc\"}"));
22+
uriVariables.put(encodeURIComponent("sort[].1"), encodeURIComponent("{\"field\":\"name\",\"order\":\"desc\"}"));
2123
uriVariables.put("recordIds.0", "rec0");
2224
uriVariables.put("recordIds.1", "rec1");
2325
String urlParam = MapUtil.extractKeyToVariables(uriVariables);
24-
assertThat(urlParam).isEqualTo("?recordIds={recordIds.0}&sort%5B%5D={sort[].0}&sort%5B%5D={sort[].1}&recordIds={recordIds.1}&pageSize={pageSize}&page={page}");
26+
assertThat(urlParam).isEqualTo("?page={page}&pageSize={pageSize}&sort%5B%5D={sort%5B%5D.0}&sort%5B%5D={sort%5B%5D.1}&recordIds={recordIds.0}&recordIds={recordIds.1}");
2527
}
2628
}

0 commit comments

Comments
 (0)