Skip to content

Commit 700a75b

Browse files
committed
Do not decode the encoded path parameter
1 parent 214c90b commit 700a75b

File tree

3 files changed

+103
-37
lines changed

3 files changed

+103
-37
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/http/Request.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public URI getUri() {
125125
if (uri.getAuthority() != null) {
126126
updatedUriString.append(uri.getAuthority());
127127
}
128-
updatedUriString.append(uri.getPath());
128+
updatedUriString.append(uri.getRawPath());
129129
if (!rawQuery.isEmpty()) {
130130
updatedUriString.append("?").append(rawQuery);
131131
}
@@ -139,9 +139,9 @@ public URI getUri() {
139139

140140
public String getRequestLine() {
141141
URI uri = getUri();
142-
String path = uri.getPath();
142+
String path = uri.getRawPath();
143143
if (!query.isEmpty()) {
144-
path += "?" + uri.getQuery();
144+
path += "?" + uri.getRawQuery();
145145
}
146146
return String.format("%s %s", method, path);
147147
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/http/EncodingTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
69

710
public class EncodingTest {
8-
@Test
9-
public void encodeMultiSegmentPathParameter() {
10-
assertEquals("/foo/bar", Encoding.encodeMultiSegmentPathParameter("/foo/bar"));
11-
assertEquals("a%3Fb%23c", Encoding.encodeMultiSegmentPathParameter("a?b#c"));
11+
12+
@ParameterizedTest(name = "encodeMultiSegmentPathParameter: {0}")
13+
@MethodSource("multiSegmentCases")
14+
public void encodeMultiSegmentPathParameter(String name, String input, String expected) {
15+
assertEquals(expected, Encoding.encodeMultiSegmentPathParameter(input));
16+
}
17+
18+
static Stream<Arguments> multiSegmentCases() {
19+
return Stream.of(
20+
Arguments.of("preserves slashes", "/foo/bar", "/foo/bar"),
21+
Arguments.of("encodes question mark and hash", "a?b#c", "a%3Fb%23c"),
22+
Arguments.of("encodes pipe", "name|here", "name%7Chere"),
23+
Arguments.of("encodes brackets", "a[b]", "a%5Bb%5D"),
24+
Arguments.of("encodes space", "a b", "a%20b"),
25+
Arguments.of("encodes braces", "a{b}", "a%7Bb%7D"),
26+
Arguments.of("preserves sub-delims", "a$b(c)d+e", "a$b(c)d+e"),
27+
Arguments.of("preserves dots and hyphens", "catalog.schema.table-name", "catalog.schema.table-name"),
28+
Arguments.of(
29+
"customer table name from issue",
30+
"catalog.schema.df$(dfs)sdf+fds-sdf0123456789_abcdefghijklmnopqrstuvwxyz|",
31+
"catalog.schema.df$(dfs)sdf+fds-sdf0123456789_abcdefghijklmnopqrstuvwxyz%7C"));
1232
}
1333
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/http/RequestTest.java

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,89 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import java.net.URI;
6-
import org.junit.jupiter.api.Test;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
710

811
class RequestTest {
912

10-
@Test
11-
void requestUriJustPath() {
12-
URI uri = new Request("GET", "/foo").getUri();
13-
assertEquals("/foo", uri.toString());
13+
@ParameterizedTest(name = "{0}")
14+
@MethodSource("getUriCases")
15+
void getUri(String name, Request request, String expectedUri) {
16+
URI uri = request.getUri();
17+
assertEquals(expectedUri, uri.toString());
1418
}
1519

16-
@Test
17-
void requestUriWithQuery() {
18-
URI uri = new Request("GET", "/foo").withQueryParam("foo", "bar").getUri();
19-
assertEquals("/foo?foo=bar", uri.toString());
20+
static Stream<Arguments> getUriCases() {
21+
return Stream.of(
22+
Arguments.of(
23+
"just path",
24+
new Request("GET", "/foo"),
25+
"/foo"),
26+
Arguments.of(
27+
"with query param",
28+
new Request("GET", "/foo").withQueryParam("foo", "bar"),
29+
"/foo?foo=bar"),
30+
Arguments.of(
31+
"merge query into existing",
32+
new Request("GET", "/foo?api=1.0").withQueryParam("foo", "bar"),
33+
"/foo?api=1.0&foo=bar"),
34+
Arguments.of(
35+
"forward-slash encoded in query param",
36+
new Request("GET", "/foo").withQueryParam("foo", "bar/baz"),
37+
"/foo?foo=bar%2Fbaz"),
38+
Arguments.of(
39+
"multiple values for same query param",
40+
new Request("GET", "/foo")
41+
.withQueryParam("foo", "bar")
42+
.withQueryParam("foo", "baz"),
43+
"/foo?foo=bar&foo=baz"),
44+
Arguments.of(
45+
"pre-encoded pipe in path",
46+
new Request("GET", String.format("/api/2.1/tables/%s",
47+
Encoding.encodeMultiSegmentPathParameter("name|here"))),
48+
"/api/2.1/tables/name%7Chere"),
49+
Arguments.of(
50+
"pre-encoded path with query params preserves encoding",
51+
new Request("GET", String.format("/api/2.1/tables/%s",
52+
Encoding.encodeMultiSegmentPathParameter("name|here")))
53+
.withQueryParam("key", "value"),
54+
"/api/2.1/tables/name%7Chere?key=value"),
55+
Arguments.of(
56+
"pre-encoded path with full URL preserves encoding",
57+
new Request("GET", "/ignored")
58+
.withUrl("https://host.example.com" + String.format("/api/2.1/tables/%s",
59+
Encoding.encodeMultiSegmentPathParameter("name|here"))),
60+
"https://host.example.com/api/2.1/tables/name%7Chere"));
2061
}
2162

22-
@Test
23-
void requestUriMergeQuery() {
24-
URI uri = new Request("GET", "/foo?api=1.0").withQueryParam("foo", "bar").getUri();
25-
assertEquals("/foo?api=1.0&foo=bar", uri.toString());
63+
@ParameterizedTest(name = "{0}")
64+
@MethodSource("getRequestLineCases")
65+
void getRequestLine(String name, Request request, String expectedLine) {
66+
assertEquals(expectedLine, request.getRequestLine());
2667
}
2768

28-
// Test that forward-slashes are appropriately encoded in query parameters.
29-
@Test
30-
void requestUriWithQueryWithForwardSlash() {
31-
URI uri = new Request("GET", "/foo").withQueryParam("foo", "bar/baz").getUri();
32-
assertEquals("/foo?foo=bar%2Fbaz", uri.toString());
33-
}
34-
35-
// Test that multiple values for the same query parameter are appropriately encoded.
36-
@Test
37-
void requestUriWithQueryWithMultipleValues() {
38-
URI uri =
39-
new Request("GET", "/foo")
40-
.withQueryParam("foo", "bar")
41-
.withQueryParam("foo", "baz")
42-
.getUri();
43-
assertEquals("/foo?foo=bar&foo=baz", uri.toString());
69+
static Stream<Arguments> getRequestLineCases() {
70+
return Stream.of(
71+
Arguments.of(
72+
"simple path",
73+
new Request("GET", "/foo"),
74+
"GET /foo"),
75+
Arguments.of(
76+
"path with query param",
77+
new Request("GET", "/foo").withQueryParam("key", "value"),
78+
"GET /foo?key=value"),
79+
Arguments.of(
80+
"encoded path preserves encoding in request line",
81+
new Request("GET", String.format("/api/2.1/tables/%s",
82+
Encoding.encodeMultiSegmentPathParameter("name|here"))),
83+
"GET /api/2.1/tables/name%7Chere"),
84+
Arguments.of(
85+
"encoded path with query preserves encoding in request line",
86+
new Request("GET", String.format("/api/2.1/tables/%s",
87+
Encoding.encodeMultiSegmentPathParameter("name|here")))
88+
.withQueryParam("key", "val/ue"),
89+
"GET /api/2.1/tables/name%7Chere?key=val%2Fue"));
4490
}
4591
}

0 commit comments

Comments
 (0)