Skip to content

Commit 53fb026

Browse files
committed
tweaks
1 parent 19bb3d8 commit 53fb026

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

conjure-java-jaxrs-client/src/main/java/com/palantir/conjure/java/client/jaxrs/DialogueFeignClient.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ final class DialogueFeignClient implements feign.Client {
7373
private static final Splitter PATH_SPLITTER = Splitter.on('/');
7474
private static final Splitter QUERY_SPLITTER = Splitter.on('&').omitEmptyStrings();
7575
private static final char QUERY_KEY_VALUE_SEPARATOR = '=';
76-
private static final Splitter QUERY_KEY_VALUE_SPLITTER = Splitter.on(QUERY_KEY_VALUE_SEPARATOR);
7776

7877
private final ConjureRuntime runtime;
7978
private final Channel channel;
@@ -419,23 +418,18 @@ public void renderPath(ListMultimap<String, String> params, UrlBuilder url) {
419418
if (queryParamsStart != -1) {
420419
String querySegments = trailing.substring(queryParamsStart + 1);
421420
for (String querySegment : QUERY_SPLITTER.split(querySegments)) {
422-
boolean isValid = false;
423421
int equalsIndex = querySegment.indexOf(QUERY_KEY_VALUE_SEPARATOR);
424-
if (equalsIndex > -1) {
422+
if (equalsIndex > 0) {
425423
String key = querySegment.substring(0, equalsIndex);
426-
int valueStart = equalsIndex + 1;
427-
if (querySegment.indexOf(QUERY_KEY_VALUE_SEPARATOR, valueStart) == -1) {
428-
isValid = true;
429-
url.queryParam(urlDecode(key), urlDecode(querySegment.substring(valueStart)));
430-
}
431-
}
432-
433-
if (!isValid) {
434-
List<String> keyValuePair = QUERY_KEY_VALUE_SPLITTER.splitToList(querySegment);
424+
String value = querySegment.substring(equalsIndex + 1);
425+
url.queryParam(urlDecode(key), urlDecode(value));
426+
} else {
435427
throw new SafeIllegalStateException(
436428
"Expected two parameters",
437-
SafeArg.of("parameters", keyValuePair.size()),
438-
UnsafeArg.of("values", keyValuePair));
429+
SafeArg.of(
430+
"parameters",
431+
querySegment.split(String.valueOf(QUERY_KEY_VALUE_SEPARATOR)).length),
432+
UnsafeArg.of("values", querySegment));
439433
}
440434
}
441435
}

conjure-java-jaxrs-client/src/test/java/com/palantir/conjure/java/client/jaxrs/JaxRsClientDialogueEndpointTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package com.palantir.conjure.java.client.jaxrs;
1818

19+
import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy;
1920
import static org.assertj.core.api.Assertions.assertThat;
2021
import static org.assertj.core.api.Assertions.assertThatThrownBy;
22+
import static org.assertj.core.api.InstanceOfAssertFactories.collection;
2123
import static org.mockito.ArgumentMatchers.any;
2224
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.never;
2326
import static org.mockito.Mockito.times;
2427
import static org.mockito.Mockito.verify;
2528
import static org.mockito.Mockito.when;
@@ -35,6 +38,9 @@
3538
import com.palantir.dialogue.Request;
3639
import com.palantir.dialogue.Response;
3740
import com.palantir.dialogue.UrlBuilder;
41+
import com.palantir.logsafe.SafeArg;
42+
import com.palantir.logsafe.UnsafeArg;
43+
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
3844
import jakarta.ws.rs.Consumes;
3945
import jakarta.ws.rs.GET;
4046
import jakarta.ws.rs.POST;
@@ -95,6 +101,56 @@ public void testQueryParameterCollection() {
95101
verify(urlBuilder).queryParam("query", "a+b");
96102
}
97103

104+
@Test
105+
public void testQueryParameters() {
106+
Channel channel = stubNoContentResponseChannel();
107+
StubService service = JaxRsClient.create(StubService.class, channel, runtime);
108+
service.collectionOfQueryParams(List.of("", "=", "test=value"));
109+
110+
ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
111+
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
112+
verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
113+
assertThat(requestCaptor.getValue().pathParameters().asMap())
114+
.hasSize(1)
115+
.extractingByKey("request-url")
116+
.asInstanceOf(collection(String.class))
117+
.containsExactly("dialogue://feign/foo/params?query=&query=%3D&query=test%3Dvalue");
118+
Endpoint endpoint = endpointCaptor.getValue();
119+
UrlBuilder urlBuilder = mock(UrlBuilder.class);
120+
endpoint.renderPath(
121+
ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?query=&query==&query=test=value"),
122+
urlBuilder);
123+
verify(urlBuilder).queryParam("query", "");
124+
verify(urlBuilder).queryParam("query", "=");
125+
verify(urlBuilder).queryParam("query", "test=value");
126+
}
127+
128+
@Test
129+
public void testInvalidQueryParameters() {
130+
Channel channel = stubNoContentResponseChannel();
131+
StubService service = JaxRsClient.create(StubService.class, channel, runtime);
132+
service.collectionOfQueryParams(List.of());
133+
134+
ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
135+
ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
136+
verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
137+
Endpoint endpoint = endpointCaptor.getValue();
138+
UrlBuilder urlBuilder = mock(UrlBuilder.class);
139+
assertThatLoggableExceptionThrownBy(() -> endpoint.renderPath(
140+
ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?="), urlBuilder))
141+
.isInstanceOf(SafeIllegalStateException.class)
142+
.hasLogMessage("Expected two parameters")
143+
.args()
144+
.containsExactlyInAnyOrder(SafeArg.of("parameters", 0), UnsafeArg.of("values", "="));
145+
assertThatLoggableExceptionThrownBy(() -> endpoint.renderPath(
146+
ImmutableListMultimap.of("request-url", "dialogue://feign/foo/params?query"), urlBuilder))
147+
.isInstanceOf(SafeIllegalStateException.class)
148+
.hasLogMessage("Expected two parameters")
149+
.args()
150+
.containsExactlyInAnyOrder(SafeArg.of("parameters", 1), UnsafeArg.of("values", "query"));
151+
verify(urlBuilder, never()).queryParam(any(), any());
152+
}
153+
98154
@Test
99155
public void testPostWithBody() {
100156
Channel channel = stubNoContentResponseChannel();

0 commit comments

Comments
 (0)