Skip to content

Commit f16553d

Browse files
kdavisk6velo
authored andcommitted
Adding Support for Query Parameter Name Expansion (OpenFeign#841)
* Adding Support for Query Parameter Name Expansion Fixes OpenFeign#838 `QueryTemplate` assumed that all query names were literals. This change adds support for Expressions in Query Parameter names, providing better adherence to RFC 6570. RequestLines such as `@RequestLine("GET /uri?{parameter}={value}")` are now fully expanded whereas before, only `{value}` would be. * Adding Encoding and Resolution Enums for Template Control These new enums replace the boolean values used to control encoding and expression expansion options in Templates * Allow unresolved expressions in Query Parameter Name and Body Template Expressions in Query Parameter names and in a Body Template will now no longer be removed if they are not resolved. For Query Template, this change will prevent invalid query name/value pairs from being generated. For the Body Template, the documentation states that unresolved should be preserved, yet the code did not match.
1 parent f72db36 commit f16553d

8 files changed

Lines changed: 85 additions & 28 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static BodyTemplate create(String template) {
3535
}
3636

3737
private BodyTemplate(String value, Charset charset) {
38-
super(value, false, false, false, charset);
38+
super(value, ExpansionOptions.ALLOW_UNRESOLVED, EncodingOptions.NOT_REQUIRED, false, charset);
3939
}
4040

4141
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ public String getValue() {
6868
}
6969
return "{" + this.name + "}";
7070
}
71+
72+
@Override
73+
public String toString() {
74+
return this.getValue();
75+
}
7176
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static HeaderTemplate append(HeaderTemplate headerTemplate, Iterable<Stri
7979
* @param template to parse.
8080
*/
8181
private HeaderTemplate(String template, String name, Iterable<String> values, Charset charset) {
82-
super(template, false, false, false, charset);
82+
super(template, ExpansionOptions.REQUIRED, EncodingOptions.NOT_REQUIRED, false, charset);
8383
this.values = StreamSupport.stream(values.spliterator(), false)
8484
.filter(Util::isNotBlank)
8585
.collect(Collectors.toSet());

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class QueryTemplate extends Template {
3232

3333
/* cache a copy of the variables for lookup later */
3434
private List<String> values;
35-
private final String name;
35+
private final Template name;
3636
private final CollectionFormat collectionFormat;
3737
private boolean pure = false;
3838

@@ -118,8 +118,9 @@ private QueryTemplate(
118118
Iterable<String> values,
119119
Charset charset,
120120
CollectionFormat collectionFormat) {
121-
super(template, false, true, true, charset);
122-
this.name = name;
121+
super(template, ExpansionOptions.REQUIRED, EncodingOptions.REQUIRED, true, charset);
122+
this.name = new Template(name, ExpansionOptions.ALLOW_UNRESOLVED, EncodingOptions.REQUIRED,
123+
false, charset);
123124
this.collectionFormat = collectionFormat;
124125
this.values = StreamSupport.stream(values.spliterator(), false)
125126
.filter(Util::isNotBlank)
@@ -136,12 +137,12 @@ public List<String> getValues() {
136137
}
137138

138139
public String getName() {
139-
return name;
140+
return name.toString();
140141
}
141142

142143
@Override
143144
public String toString() {
144-
return this.queryString(super.toString());
145+
return this.queryString(this.name.toString(), super.toString());
145146
}
146147

147148
/**
@@ -153,12 +154,13 @@ public String toString() {
153154
*/
154155
@Override
155156
public String expand(Map<String, ?> variables) {
156-
return this.queryString(super.expand(variables));
157+
String name = this.name.expand(variables);
158+
return this.queryString(name, super.expand(variables));
157159
}
158160

159-
private String queryString(String values) {
161+
private String queryString(String name, String values) {
160162
if (this.pure) {
161-
return this.name;
163+
return name;
162164
}
163165

164166
/* covert the comma separated values into a value query string */
@@ -167,7 +169,7 @@ private String queryString(String values) {
167169
.collect(Collectors.toList());
168170

169171
if (!resolved.isEmpty()) {
170-
return this.collectionFormat.join(this.name, resolved, this.getCharset()).toString();
172+
return this.collectionFormat.join(name, resolved, this.getCharset()).toString();
171173
}
172174

173175
/* nothing to return, all values are unresolved */

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
* <a href="https://tools.ietf.org/html/rfc6570">RFC 6570</a>, with some relaxed rules, allowing the
2929
* concept to be used in areas outside of the uri.
3030
*/
31-
public abstract class Template {
31+
public class Template {
3232

3333
private static final Logger logger = Logger.getLogger(Template.class.getName());
3434
private static final Pattern QUERY_STRING_PATTERN = Pattern.compile("(?<!\\{)(\\?)");
3535
private final String template;
3636
private final boolean allowUnresolved;
37-
private final boolean encode;
37+
private final EncodingOptions encode;
3838
private final boolean encodeSlash;
3939
private final Charset charset;
4040
private final List<TemplateChunk> templateChunks = new ArrayList<>();
@@ -48,12 +48,13 @@ public abstract class Template {
4848
* @param encodeSlash if slash characters should be encoded.
4949
*/
5050
Template(
51-
String value, boolean allowUnresolved, boolean encode, boolean encodeSlash, Charset charset) {
51+
String value, ExpansionOptions allowUnresolved, EncodingOptions encode, boolean encodeSlash,
52+
Charset charset) {
5253
if (value == null) {
5354
throw new IllegalArgumentException("template is required.");
5455
}
5556
this.template = value;
56-
this.allowUnresolved = allowUnresolved;
57+
this.allowUnresolved = ExpansionOptions.ALLOW_UNRESOLVED == allowUnresolved;
5758
this.encode = encode;
5859
this.encodeSlash = encodeSlash;
5960
this.charset = charset;
@@ -78,7 +79,7 @@ public String expand(Map<String, ?> variables) {
7879
Expression expression = (Expression) chunk;
7980
Object value = variables.get(expression.getName());
8081
if (value != null) {
81-
String expanded = expression.expand(value, this.encode);
82+
String expanded = expression.expand(value, this.encode.isEncodingRequired());
8283
if (!this.encodeSlash) {
8384
logger.fine("Explicit slash decoding specified, decoding all slashes in uri");
8485
expanded = expanded.replaceAll("\\%2F", "/");
@@ -105,7 +106,7 @@ public String expand(Map<String, ?> variables) {
105106
* @return the encoded value.
106107
*/
107108
private String encode(String value) {
108-
return this.encode ? UriUtils.encode(value, this.charset) : value;
109+
return this.encode.isEncodingRequired() ? UriUtils.encode(value, this.charset) : value;
109110
}
110111

111112
/**
@@ -116,7 +117,7 @@ private String encode(String value) {
116117
* @return the encoded value
117118
*/
118119
private String encode(String value, boolean query) {
119-
if (this.encode) {
120+
if (this.encode.isEncodingRequired()) {
120121
return query ? UriUtils.queryEncode(value, this.charset)
121122
: UriUtils.pathEncode(value, this.charset);
122123
} else {
@@ -218,15 +219,11 @@ public String toString() {
218219
.map(TemplateChunk::getValue).collect(Collectors.joining());
219220
}
220221

221-
public boolean allowUnresolved() {
222-
return allowUnresolved;
223-
}
224-
225222
public boolean encode() {
226-
return encode;
223+
return encode.isEncodingRequired();
227224
}
228225

229-
public boolean encodeSlash() {
226+
boolean encodeSlash() {
230227
return encodeSlash;
231228
}
232229

@@ -315,4 +312,22 @@ public String next() {
315312
}
316313
}
317314

315+
public enum EncodingOptions {
316+
REQUIRED(true), NOT_REQUIRED(false);
317+
318+
private boolean shouldEncode;
319+
320+
EncodingOptions(boolean shouldEncode) {
321+
this.shouldEncode = shouldEncode;
322+
}
323+
324+
public boolean isEncodingRequired() {
325+
return this.shouldEncode;
326+
}
327+
}
328+
329+
public enum ExpansionOptions {
330+
ALLOW_UNRESOLVED, REQUIRED
331+
}
332+
318333
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public static UriTemplate append(UriTemplate uriTemplate, String fragment) {
7070
* @param charset to use when encoding.
7171
*/
7272
private UriTemplate(String template, boolean encodeSlash, Charset charset) {
73-
super(template, false, true, encodeSlash, charset);
73+
super(template, ExpansionOptions.REQUIRED, EncodingOptions.REQUIRED, encodeSlash, charset);
7474
}
7575
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.google.gson.reflect.TypeToken;
1717
import java.util.ArrayList;
18+
import java.util.Collections;
1819
import org.assertj.core.api.Fail;
1920
import org.junit.Rule;
2021
import org.junit.Test;
@@ -147,7 +148,8 @@ public void headersOnMethodAddsContentTypeHeader() throws Exception {
147148
assertThat(md.template())
148149
.hasHeaders(
149150
entry("Content-Type", asList("application/xml")),
150-
entry("Content-Length", asList(String.valueOf(md.template().body().length))));
151+
entry("Content-Length",
152+
asList(String.valueOf(md.template().requestBody().asBytes().length))));
151153
}
152154

153155
@Test
@@ -157,7 +159,8 @@ public void headersOnTypeAddsContentTypeHeader() throws Exception {
157159
assertThat(md.template())
158160
.hasHeaders(
159161
entry("Content-Type", asList("application/xml")),
160-
entry("Content-Length", asList(String.valueOf(md.template().body().length))));
162+
entry("Content-Length",
163+
asList(String.valueOf(md.template().requestBody().asBytes().length))));
161164
}
162165

163166
@Test
@@ -167,7 +170,8 @@ public void headersContainsWhitespaces() throws Exception {
167170
assertThat(md.template())
168171
.hasHeaders(
169172
entry("Content-Type", asList("application/xml")),
170-
entry("Content-Length", asList(String.valueOf(md.template().body().length))));
173+
entry("Content-Length",
174+
asList(String.valueOf(md.template().requestBody().asBytes().length))));
171175
}
172176

173177
@Test

‎core/src/test/java/feign/template/QueryTemplateTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,35 @@ public void collectionFormat() {
7171
assertThat(expanded).isEqualToIgnoringCase("name=James,Jason");
7272
}
7373

74+
@Test
75+
public void expandName() {
76+
QueryTemplate template =
77+
QueryTemplate.create("{name}", Arrays.asList("James", "Jason"), Util.UTF_8);
78+
String expanded = template.expand(Collections.singletonMap("name", "firsts"));
79+
assertThat(expanded).isEqualToIgnoringCase("firsts=James&firsts=Jason");
80+
}
81+
82+
@Test
83+
public void expandPureParameter() {
84+
QueryTemplate template =
85+
QueryTemplate.create("{name}", Collections.emptyList(), Util.UTF_8);
86+
String expanded = template.expand(Collections.singletonMap("name", "firsts"));
87+
assertThat(expanded).isEqualToIgnoringCase("firsts");
88+
}
89+
90+
@Test
91+
public void expandPureParameterWithSlash() {
92+
QueryTemplate template =
93+
QueryTemplate.create("/path/{name}", Collections.emptyList(), Util.UTF_8);
94+
String expanded = template.expand(Collections.singletonMap("name", "firsts"));
95+
assertThat(expanded).isEqualToIgnoringCase("/path/firsts");
96+
}
97+
98+
@Test
99+
public void expandNameUnresolved() {
100+
QueryTemplate template =
101+
QueryTemplate.create("{parameter}", Arrays.asList("James", "Jason"), Util.UTF_8);
102+
String expanded = template.expand(Collections.singletonMap("name", "firsts"));
103+
assertThat(expanded).isEqualToIgnoringCase("%7Bparameter%7D=James&%7Bparameter%7D=Jason");
104+
}
74105
}

0 commit comments

Comments
 (0)