Skip to content

Commit 57444d1

Browse files
author
Adrian Cole
committed
Fixes vnd header regression by changing Headers encoding
I mistakenly advised `@Headers` to follow the encoding rules of `@Body`. This was a a mistake as in both cases, url encoding is a bad choice, if the only goal is to prevent accidental variable expansion. For example, url encoding interferes a lot with content, including messing with '+' characters, such as exist in "Accept: application/vnd.github.v3+json" This changes `@Headers` to only address the problem, which where a '{' literal is desired in a header value. The solution offered here is to simply repeat "{{" when you desire a '{' literal. For example, if your header value needs to be literally "{{variable}}", you'd encode it as "{{{{variable}}". The impact of this change is limited to those who have already started using v8.15, and a fast release will occur after merge to limit that. See OpenFeign#326 Fixes OpenFeign#345 Closes OpenFeign#346
1 parent b29f9d8 commit 57444d1

2 files changed

Lines changed: 36 additions & 17 deletions

File tree

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public final class RequestTemplate implements Serializable {
6060
private boolean decodeSlash = true;
6161

6262
public RequestTemplate() {
63-
6463
}
6564

6665
/* Copy constructor. Use this when making templates. */
@@ -128,9 +127,19 @@ public static String expand(String template, Map<String, ?> variables) {
128127
for (char c : template.toCharArray()) {
129128
switch (c) {
130129
case '{':
130+
if (inVar) {
131+
// '{{' is an escape: write the brace and don't interpret as a variable
132+
builder.append("{");
133+
inVar = false;
134+
break;
135+
}
131136
inVar = true;
132137
break;
133138
case '}':
139+
if (!inVar) { // then write the brace literally
140+
builder.append('}');
141+
break;
142+
}
134143
inVar = false;
135144
String key = var.toString();
136145
Object value = variables.get(var.toString());
@@ -209,13 +218,11 @@ public RequestTemplate resolve(Map<String, ?> unencoded) {
209218
}
210219
url = new StringBuilder(resolvedUrl);
211220

212-
Map<String, Collection<String>>
213-
resolvedHeaders =
214-
new LinkedHashMap<String, Collection<String>>();
221+
Map<String, Collection<String>> resolvedHeaders = new LinkedHashMap<String, Collection<String>>();
215222
for (String field : headers.keySet()) {
216223
Collection<String> resolvedValues = new ArrayList<String>();
217224
for (String value : valuesOrEmpty(headers, field)) {
218-
String resolved = urlDecode(expand(value, encoded));
225+
String resolved = expand(value, unencoded);
219226
resolvedValues.add(resolved);
220227
}
221228
resolvedHeaders.put(field, resolvedValues);
@@ -285,37 +292,37 @@ public String url() {
285292
}
286293

287294
/**
288-
* Replaces queries with the specified {@code configKey} with url decoded {@code values} supplied.
295+
* Replaces queries with the specified {@code name} with url decoded {@code values} supplied.
289296
* <br> When the {@code value} is {@code null}, all queries with the {@code configKey} are
290297
* removed. <br> <br><br><b>relationship to JAXRS 2.0</b><br> <br> Like {@code WebTarget.query},
291298
* except the values can be templatized. <br> ex. <br>
292299
* <pre>
293300
* template.query(&quot;Signature&quot;, &quot;{signature}&quot;);
294301
* </pre>
295302
*
296-
* @param configKey the configKey of the query
303+
* @param name the name of the query
297304
* @param values can be a single null to imply removing all values. Else no values are expected
298305
* to be null.
299306
* @see #queries()
300307
*/
301-
public RequestTemplate query(String configKey, String... values) {
302-
queries.remove(checkNotNull(configKey, "configKey"));
308+
public RequestTemplate query(String name, String... values) {
309+
queries.remove(checkNotNull(name, "name"));
303310
if (values != null && values.length > 0 && values[0] != null) {
304311
ArrayList<String> encoded = new ArrayList<String>();
305312
for (String value : values) {
306313
encoded.add(encodeIfNotVariable(value));
307314
}
308-
this.queries.put(encodeIfNotVariable(configKey), encoded);
315+
this.queries.put(encodeIfNotVariable(name), encoded);
309316
}
310317
return this;
311318
}
312319

313320
/* @see #query(String, String...) */
314-
public RequestTemplate query(String configKey, Iterable<String> values) {
321+
public RequestTemplate query(String name, Iterable<String> values) {
315322
if (values != null) {
316-
return query(configKey, toArray(values, String.class));
323+
return query(name, toArray(values, String.class));
317324
}
318-
return query(configKey, (String[]) null);
325+
return query(name, (String[]) null);
319326
}
320327

321328
private String encodeIfNotVariable(String in) {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,26 @@ public void resolveTemplateWithHeaderSubstitutionsNotAtStart() {
154154
}
155155

156156
@Test
157-
public void resolveTemplateWithHeaderWithURLEncodedElements() {
157+
public void resolveTemplateWithHeaderWithEscapedCurlyBrace() {
158158
RequestTemplate template = new RequestTemplate().method("GET")
159-
.header("Encoded", "%7Bvar%7D");
159+
.header("Encoded", "{{{{dont_expand_me}}");
160160

161-
template.resolve(mapOf("var", "1234"));
161+
template.resolve(mapOf("dont_expand_me", "1234"));
162162

163163
assertThat(template)
164-
.hasHeaders(entry("Encoded", asList("{var}")));
164+
.hasHeaders(entry("Encoded", asList("{{dont_expand_me}}")));
165+
}
166+
167+
/** This ensures we don't mess up vnd types */
168+
@Test
169+
public void resolveTemplateWithHeaderIncludingSpecialCharacters() {
170+
RequestTemplate template = new RequestTemplate().method("GET")
171+
.header("Accept", "application/vnd.github.v3+{type}");
172+
173+
template.resolve(mapOf("type", "json"));
174+
175+
assertThat(template)
176+
.hasHeaders(entry("Accept", asList("application/vnd.github.v3+json")));
165177
}
166178

167179
@Test

0 commit comments

Comments
 (0)