Skip to content

Commit 968f35f

Browse files
koiuoadriancole
authored andcommitted
Adds support for encoded query parameters in @QueryMap (OpenFeign#408)
Adds support for encoded query parameters in `@QueryMap` via `@QueryMap(encoded = true)`
1 parent 44afa41 commit 968f35f

8 files changed

Lines changed: 114 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Content-Length response bodies with lengths greater than Integer.MAX_VALUE report null length
88
* Previously the OkhttpClient would throw an exception, and ApacheHttpClient
99
would report a wrong, possibly negative value
10+
* Adds support for encoded query parameters in `@QueryMap` via `@QueryMap(encoded = true)`
1011

1112
### Version 8.17
1213
* Adds support to RxJava Completable via `HystrixFeign` builder with fallback support

‎core/src/main/java/feign/Contract.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[
265265
} else if (annotationType == QueryMap.class) {
266266
checkState(data.queryMapIndex() == null, "QueryMap annotation was present on multiple parameters.");
267267
data.queryMapIndex(paramIndex);
268+
data.queryMapEncoded(QueryMap.class.cast(annotation).encoded());
268269
isHttpAnnotation = true;
269270
} else if (annotationType == HeaderMap.class) {
270271
checkState(data.headerMapIndex() == null, "HeaderMap annotation was present on multiple parameters.");

‎core/src/main/java/feign/MethodMetadata.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class MethodMetadata implements Serializable {
3434
private Integer bodyIndex;
3535
private Integer headerMapIndex;
3636
private Integer queryMapIndex;
37+
private boolean queryMapEncoded;
3738
private transient Type bodyType;
3839
private RequestTemplate template = new RequestTemplate();
3940
private List<String> formParams = new ArrayList<String>();
@@ -103,6 +104,15 @@ public MethodMetadata queryMapIndex(Integer queryMapIndex) {
103104
return this;
104105
}
105106

107+
public boolean queryMapEncoded() {
108+
return queryMapEncoded;
109+
}
110+
111+
public MethodMetadata queryMapEncoded(boolean queryMapEncoded) {
112+
this.queryMapEncoded = queryMapEncoded;
113+
return this;
114+
}
115+
106116
/**
107117
* Type corresponding to {@link #bodyIndex()}.
108118
*/

‎core/src/main/java/feign/QueryMap.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@
6060
@Retention(RUNTIME)
6161
@java.lang.annotation.Target(PARAMETER)
6262
public @interface QueryMap {
63+
/** Specifies whether parameter names and values are already encoded. */
64+
boolean encoded() default false;
6365
}

‎core/src/main/java/feign/ReflectiveFeign.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private RequestTemplate addQueryMapQueryParameters(Object[] argv, RequestTemplat
284284
values.add(currValue == null ? null : currValue.toString());
285285
}
286286

287-
mutable.query((String) currEntry.getKey(), values);
287+
mutable.query(metadata.queryMapEncoded(), (String) currEntry.getKey(), values);
288288
}
289289
return mutable;
290290
}

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

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ public String method() {
260260
}
261261

262262
public RequestTemplate decodeSlash(boolean decodeSlash) {
263-
this.decodeSlash = decodeSlash;
264-
return this;
263+
this.decodeSlash = decodeSlash;
264+
return this;
265265
}
266266

267267
public boolean decodeSlash() {
268-
return decodeSlash;
268+
return decodeSlash;
269269
}
270-
270+
271271
/* @see #url() */
272272
public RequestTemplate append(CharSequence value) {
273273
url.append(value);
@@ -292,41 +292,76 @@ public String url() {
292292
}
293293

294294
/**
295-
* Replaces queries with the specified {@code name} with url decoded {@code values} supplied.
295+
* Replaces queries with the specified {@code name} with the {@code values} supplied.
296+
* <br> Values can be passed in decoded or in url-encoded form depending on the value of the
297+
* {@code encoded} parameter.
296298
* <br> When the {@code value} is {@code null}, all queries with the {@code configKey} are
297299
* removed. <br> <br><br><b>relationship to JAXRS 2.0</b><br> <br> Like {@code WebTarget.query},
298300
* except the values can be templatized. <br> ex. <br>
299301
* <pre>
300302
* template.query(&quot;Signature&quot;, &quot;{signature}&quot;);
301303
* </pre>
304+
* <br> <b>Note:</b> behavior of RequestTemplate is not consistent if a query parameter with
305+
* unsafe characters is passed as both encoded and unencoded, although no validation is performed.
306+
* <br> ex. <br>
307+
* <pre>
308+
* template.query(true, &quot;param[]&quot;, &quot;value&quot;);
309+
* template.query(false, &quot;param[]&quot;, &quot;value&quot;);
310+
* </pre>
302311
*
303-
* @param name the name of the query
312+
* @param encoded whether name and values are already url-encoded
313+
* @param name the name of the query
304314
* @param values can be a single null to imply removing all values. Else no values are expected
305315
* to be null.
306316
* @see #queries()
307317
*/
318+
public RequestTemplate query(boolean encoded, String name, String... values) {
319+
return doQuery(encoded, name, values);
320+
}
321+
322+
/* @see #query(boolean, String, String...) */
323+
public RequestTemplate query(boolean encoded, String name, Iterable<String> values) {
324+
return doQuery(encoded, name, values);
325+
}
326+
327+
/**
328+
* Shortcut for {@code query(false, String, String...)}
329+
* @see #query(boolean, String, String...)
330+
*/
308331
public RequestTemplate query(String name, String... values) {
309-
String encodedName = encodeIfNotVariable(checkNotNull(name, "name"));
310-
queries.remove(encodedName);
332+
return doQuery(false, name, values);
333+
}
334+
335+
/**
336+
* Shortcut for {@code query(false, String, Iterable<String>)}
337+
* @see #query(boolean, String, String...)
338+
*/
339+
public RequestTemplate query(String name, Iterable<String> values) {
340+
return doQuery(false, name, values);
341+
}
342+
343+
private RequestTemplate doQuery(boolean encoded, String name, String... values) {
344+
checkNotNull(name, "name");
345+
String paramName = encoded ? name : encodeIfNotVariable(name);
346+
queries.remove(paramName);
311347
if (values != null && values.length > 0 && values[0] != null) {
312-
ArrayList<String> encoded = new ArrayList<String>();
348+
ArrayList<String> paramValues = new ArrayList<String>();
313349
for (String value : values) {
314-
encoded.add(encodeIfNotVariable(value));
350+
paramValues.add(encoded ? value : encodeIfNotVariable(value));
315351
}
316-
this.queries.put(encodedName, encoded);
352+
this.queries.put(paramName, paramValues);
317353
}
318354
return this;
319355
}
320356

321-
/* @see #query(String, String...) */
322-
public RequestTemplate query(String name, Iterable<String> values) {
357+
private RequestTemplate doQuery(boolean encoded, String name, Iterable<String> values) {
323358
if (values != null) {
324-
return query(name, toArray(values, String.class));
359+
return doQuery(encoded, name, toArray(values, String.class));
325360
}
326-
return query(name, (String[]) null);
361+
return doQuery(encoded, name, (String[]) null);
327362
}
328363

329-
private String encodeIfNotVariable(String in) {
364+
private static String encodeIfNotVariable(String in) {
330365
if (in == null || in.indexOf('{') == 0) {
331366
return in;
332367
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ public void queryMap() throws Exception {
269269
assertThat(md.queryMapIndex()).isEqualTo(0);
270270
}
271271

272+
@Test
273+
public void queryMapEncodedDefault() throws Exception {
274+
MethodMetadata md = parseAndValidateMetadata(QueryMapTestInterface.class, "queryMap", Map.class);
275+
276+
assertThat(md.queryMapEncoded()).isFalse();
277+
}
278+
279+
@Test
280+
public void queryMapEncodedTrue() throws Exception {
281+
MethodMetadata md = parseAndValidateMetadata(QueryMapTestInterface.class, "queryMapEncoded", Map.class);
282+
283+
assertThat(md.queryMapEncoded()).isTrue();
284+
}
285+
286+
@Test
287+
public void queryMapEncodedFalse() throws Exception {
288+
MethodMetadata md = parseAndValidateMetadata(QueryMapTestInterface.class, "queryMapNotEncoded", Map.class);
289+
290+
assertThat(md.queryMapEncoded()).isFalse();
291+
}
292+
272293
@Test
273294
public void queryMapMapSubclass() throws Exception {
274295
MethodMetadata md = parseAndValidateMetadata(QueryMapTestInterface.class, "queryMapMapSubclass", SortedMap.class);
@@ -452,6 +473,12 @@ interface QueryMapTestInterface {
452473
@RequestLine("POST /")
453474
void queryMapMapSubclass(@QueryMap SortedMap<String, String> queryMap);
454475

476+
@RequestLine("POST /")
477+
void queryMapEncoded(@QueryMap(encoded = true) Map<String, String> queryMap);
478+
479+
@RequestLine("POST /")
480+
void queryMapNotEncoded(@QueryMap(encoded = false) Map<String, String> queryMap);
481+
455482
// invalid
456483
@RequestLine("POST /")
457484
void multipleQueryMap(@QueryMap Map<String, String> mapOne, @QueryMap Map<String, String> mapTwo);

‎core/src/test/java/feign/RequestTemplateTest.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
import org.junit.Rule;
1919
import org.junit.Test;
20+
import org.junit.rules.ExpectedException;
2021

2122
import java.util.Arrays;
2223
import java.util.LinkedHashMap;
2324
import java.util.Map;
24-
import org.junit.rules.ExpectedException;
2525

2626
import static feign.RequestTemplate.expand;
2727
import static feign.assertj.FeignAssertions.assertThat;
@@ -343,4 +343,24 @@ public void encodedQueryClearedOnNull() throws Exception {
343343
template.query("param[]", (String[]) null);
344344
assertThat(template.queries()).isEmpty();
345345
}
346+
347+
@Test
348+
public void encodedQuery() throws Exception {
349+
RequestTemplate template = new RequestTemplate().query(true, "params[]", "foo%20bar");
350+
351+
assertThat(template.queryLine()).isEqualTo("?params[]=foo%20bar");
352+
assertThat(template).hasQueries(entry("params[]", asList("foo bar")));
353+
}
354+
355+
@Test
356+
public void encodedQueryWithUnsafeCharactersMixedWithUnencoded() throws Exception {
357+
RequestTemplate template = new RequestTemplate()
358+
.query(false, "params[]", "not encoded") // stored as "param%5D%5B"
359+
.query(true, "params[]", "encoded"); // stored as "param[]"
360+
361+
// We can't ensure consistent behavior, because decode("param[]") == decode("param%5B%5D")
362+
assertThat(template.queryLine()).isEqualTo("?params%5B%5D=not+encoded&params[]=encoded");
363+
assertThat(template.queries()).doesNotContain(entry("params[]", asList("not encoded")));
364+
assertThat(template.queries()).contains(entry("params[]", asList("encoded")));
365+
}
346366
}

0 commit comments

Comments
 (0)