Skip to content

Commit 6c4dfbd

Browse files
authored
Removed decoding from Body Template Expansion (OpenFeign#931)
Removed decoding from Body Template Expansion Fixes OpenFeign#916 In certain cases, a Body Template will contain a JSON payload. To support this we are asking users to pct-encode the beginning and the end of the JSON object when providing it to the RequestLine so we don't reject it as an expression. Doing this requires that the we decode those markers before submitting the request. This change updates that logic to only decode the first and last characters only and not decode the entire payload, since Body values don't require any type of encoding.
1 parent 10a4ead commit 6c4dfbd

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

‎core/src/main/java/feign/template/BodyTemplate.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
*/
2424
public final class BodyTemplate extends Template {
2525

26+
private static final String JSON_TOKEN_START = "{";
27+
private static final String JSON_TOKEN_END = "}";
28+
private static final String JSON_TOKEN_START_ENCODED = "%7B";
29+
private static final String JSON_TOKEN_END_ENCODED = "%7D";
30+
private boolean json = false;
31+
2632
/**
2733
* Create a new Body Template.
2834
*
@@ -35,10 +41,26 @@ public static BodyTemplate create(String template) {
3541

3642
private BodyTemplate(String value, Charset charset) {
3743
super(value, ExpansionOptions.ALLOW_UNRESOLVED, EncodingOptions.NOT_REQUIRED, false, charset);
44+
if (value.startsWith(JSON_TOKEN_START_ENCODED) && value.endsWith(JSON_TOKEN_END_ENCODED)) {
45+
this.json = true;
46+
}
3847
}
3948

4049
@Override
4150
public String expand(Map<String, ?> variables) {
42-
return UriUtils.decode(super.expand(variables), Util.UTF_8);
51+
String expanded = super.expand(variables);
52+
if (this.json) {
53+
/* decode only the first and last character */
54+
StringBuilder sb = new StringBuilder();
55+
sb.append(JSON_TOKEN_START);
56+
sb.append(expanded,
57+
expanded.indexOf(JSON_TOKEN_START_ENCODED) + JSON_TOKEN_START_ENCODED.length(),
58+
expanded.lastIndexOf(JSON_TOKEN_END_ENCODED));
59+
sb.append(JSON_TOKEN_END);
60+
return sb.toString();
61+
}
62+
return expanded;
4363
}
64+
65+
4466
}

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
import static feign.assertj.FeignAssertions.assertThat;
1717
import static java.util.Arrays.asList;
1818
import static org.assertj.core.data.MapEntry.entry;
19-
import static org.junit.Assert.*;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertTrue;
2022
import feign.Request.HttpMethod;
2123
import feign.template.UriUtils;
22-
import java.util.*;
24+
import java.util.Arrays;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.LinkedHashMap;
28+
import java.util.Map;
2329
import org.junit.Rule;
2430
import org.junit.Test;
2531
import org.junit.rules.ExpectedException;
@@ -97,8 +103,7 @@ public void resolveTemplateWithParameterizedPathSkipsEncodingSlash() {
97103
public void resolveTemplateWithBinaryBody() {
98104
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)
99105
.uri("{zoneId}")
100-
.body(new byte[] {7, 3, -3, -7}, null);
101-
106+
.body(Request.Body.encoded(new byte[] {7, 3, -3, -7}, null));
102107
template = template.resolve(mapOf("zoneId", "/hostedzone/Z1PA6795UKMFR9"));
103108

104109
assertThat(template)
@@ -185,7 +190,9 @@ public void resolveTemplateWithHeaderWithEscapedCurlyBrace() {
185190
.hasHeaders(entry("Encoded", Collections.singletonList("{{{{dont_expand_me}}")));
186191
}
187192

188-
/** This ensures we don't mess up vnd types */
193+
/**
194+
* This ensures we don't mess up vnd types
195+
*/
189196
@Test
190197
public void resolveTemplateWithHeaderIncludingSpecialCharacters() {
191198
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)
@@ -244,9 +251,10 @@ public void insertHasQueryParams() {
244251
@Test
245252
public void resolveTemplateWithBodyTemplateSetsBodyAndContentLength() {
246253
RequestTemplate template = new RequestTemplate().method(HttpMethod.POST)
247-
.bodyTemplate(
254+
.body(Request.Body.bodyTemplate(
248255
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", " +
249-
"\"password\": \"{password}\"%7D");
256+
"\"password\": \"{password}\"%7D",
257+
Util.UTF_8));
250258

251259
template = template.resolve(
252260
mapOf(
@@ -259,14 +267,15 @@ public void resolveTemplateWithBodyTemplateSetsBodyAndContentLength() {
259267
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"password\"}")
260268
.hasHeaders(
261269
entry("Content-Length",
262-
Collections.singletonList(String.valueOf(template.body().length))));
270+
Collections.singletonList(String.valueOf(template.requestBody().length()))));
263271
}
264272

265273
@Test
266274
public void resolveTemplateWithBodyTemplateDoesNotDoubleDecode() {
267275
RequestTemplate template = new RequestTemplate().method(HttpMethod.POST)
268-
.bodyTemplate(
269-
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D");
276+
.body(Request.Body.bodyTemplate(
277+
"%7B\"customer_name\": \"{customer_name}\", \"user_name\": \"{user_name}\", \"password\": \"{password}\"%7D",
278+
Util.UTF_8));
270279

271280
template = template.resolve(
272281
mapOf(
@@ -276,7 +285,7 @@ public void resolveTemplateWithBodyTemplateDoesNotDoubleDecode() {
276285

277286
assertThat(template)
278287
.hasBody(
279-
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"abc 123%d8\"}");
288+
"{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"abc+123%25d8\"}");
280289
}
281290

282291
@Test
@@ -353,7 +362,9 @@ public void encodeSlashTest() {
353362
.hasUrl("/api/%2F");
354363
}
355364

356-
/** Implementations have a bug if they pass junk as the http method. */
365+
/**
366+
* Implementations have a bug if they pass junk as the http method.
367+
*/
357368
@SuppressWarnings("deprecation")
358369
@Test
359370
public void uriStuffedIntoMethod() {

0 commit comments

Comments
 (0)