Skip to content

Commit 4577082

Browse files
committed
Supports query params without values
Fixes NPE when building a client with a query param with no values
1 parent 712a8c8 commit 4577082

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Adds Request.Options support to RibbonClient
88
* Updates to Ribbon 2.0-RC13
99
* Updates to Jackson 2.5.1
10+
* Supports query parameters without values
1011

1112
### Version 7.2
1213
* Adds `Feign.Builder.build()`

core/src/main/java/feign/RequestTemplate.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ private static Map<String, Collection<String>> parseAndDecodeQueries(String quer
145145
return map;
146146
}
147147
if (queryLine.indexOf('&') == -1) {
148-
if (queryLine.indexOf('=') != -1) {
149-
putKV(queryLine, map);
150-
} else {
151-
map.put(queryLine, null);
152-
}
148+
putKV(queryLine, map);
153149
} else {
154150
char[] chars = queryLine.toCharArray();
155151
int start = 0;
@@ -504,7 +500,7 @@ private StringBuilder pullAnyQueriesOutOfUrl(StringBuilder url) {
504500
}
505501

506502
private boolean allValuesAreNull(Collection<String> values) {
507-
if (values.isEmpty()) {
503+
if (values == null || values.isEmpty()) {
508504
return true;
509505
}
510506
for (String val : values) {

core/src/main/java/feign/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
139139
* Returns an unmodifiable collection which may be empty, but is never null.
140140
*/
141141
public static <T> Collection<T> valuesOrEmpty(Map<String, Collection<T>> map, String key) {
142-
return map.containsKey(key) ? map.get(key) : Collections.<T>emptyList();
142+
return map.containsKey(key) && map.get(key) != null ? map.get(key) : Collections.<T>emptyList();
143143
}
144144

145145
public static void ensureClosed(Closeable closeable) {

core/src/test/java/feign/DefaultContractTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.rules.ExpectedException;
2323

2424
import java.net.URI;
25+
import java.util.Collections;
2526
import java.util.Date;
2627
import java.util.List;
2728

@@ -124,14 +125,31 @@ public void queryParamsInPathExtract() throws Exception {
124125
);
125126

126127
assertThat(
127-
contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("empty"))
128+
contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("twoAndOneEmpty"))
128129
.template())
129130
.hasUrl("/")
130131
.hasQueries(
131132
entry("flag", asList(new String[]{null})),
132133
entry("Action", asList("GetUser")),
133134
entry("Version", asList("2010-05-08"))
134135
);
136+
137+
assertThat(
138+
contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("oneEmpty"))
139+
.template())
140+
.hasUrl("/")
141+
.hasQueries(
142+
entry("flag", asList(new String[]{null}))
143+
);
144+
145+
assertThat(
146+
contract.parseAndValidatateMetadata(WithQueryParamsInPath.class.getDeclaredMethod("twoEmpty"))
147+
.template())
148+
.hasUrl("/")
149+
.hasQueries(
150+
entry("flag", asList(new String[]{null})),
151+
entry("NoErrors", asList(new String[]{null}))
152+
);
135153
}
136154

137155
@Test
@@ -307,7 +325,13 @@ interface WithQueryParamsInPath {
307325
Response three();
308326

309327
@RequestLine("GET /?flag&Action=GetUser&Version=2010-05-08")
310-
Response empty();
328+
Response twoAndOneEmpty();
329+
330+
@RequestLine("GET /?flag")
331+
Response oneEmpty();
332+
333+
@RequestLine("GET /?flag&NoErrors")
334+
Response twoEmpty();
311335
}
312336

313337
interface BodyWithoutParameters {

0 commit comments

Comments
 (0)