Skip to content

Commit 318fb0e

Browse files
authored
Updated Expression Patterns to allow brackets (OpenFeign#939)
* Updated Expression Patterns to allow brackets Fixes OpenFeign#928 Relaxed the regular expression that is used to determine if a given value is an Expression per the URI Template Spec RFC 6570. We already deviated by allowing dashes to exist without pct-encoding, this change adds braces `[]` to this list. Also included is the ability to set Collection Format per Query, overriding the Template default. This allows for mixed Collection formats in the same template and provides a way for Contract extensions to determine which expansion type they want when parsing a contract. * Fixing Formatting
1 parent 6771866 commit 318fb0e

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ public RequestTemplate query(String name, String... values) {
560560
return query(name, Arrays.asList(values));
561561
}
562562

563+
563564
/**
564565
* Specify a Query String parameter, with the specified values. Values can be literals or template
565566
* expressions.
@@ -569,17 +570,35 @@ public RequestTemplate query(String name, String... values) {
569570
* @return a RequestTemplate for chaining.
570571
*/
571572
public RequestTemplate query(String name, Iterable<String> values) {
572-
return appendQuery(name, values);
573+
return appendQuery(name, values, this.collectionFormat);
574+
}
575+
576+
/**
577+
* Specify a Query String parameter, with the specified values. Values can be literals or template
578+
* expressions.
579+
*
580+
* @param name of the parameter.
581+
* @param values for this parameter.
582+
* @param collectionFormat to use when resolving collection based expressions.
583+
* @return a Request Template for chaining.
584+
*/
585+
public RequestTemplate query(String name,
586+
Iterable<String> values,
587+
CollectionFormat collectionFormat) {
588+
return appendQuery(name, values, collectionFormat);
573589
}
574590

575591
/**
576592
* Appends the query name and values.
577593
*
578594
* @param name of the parameter.
579595
* @param values for the parameter, may be expressions.
596+
* @param collectionFormat to use when resolving collection based query variables.
580597
* @return a RequestTemplate for chaining.
581598
*/
582-
private RequestTemplate appendQuery(String name, Iterable<String> values) {
599+
private RequestTemplate appendQuery(String name,
600+
Iterable<String> values,
601+
CollectionFormat collectionFormat) {
583602
if (!values.iterator().hasNext()) {
584603
/* empty value, clear the existing values */
585604
this.queries.remove(name);
@@ -589,12 +608,11 @@ private RequestTemplate appendQuery(String name, Iterable<String> values) {
589608
/* create a new query template out of the information here */
590609
this.queries.compute(name, (key, queryTemplate) -> {
591610
if (queryTemplate == null) {
592-
return QueryTemplate.create(name, values, this.charset, this.collectionFormat);
611+
return QueryTemplate.create(name, values, this.charset, collectionFormat);
593612
} else {
594-
return QueryTemplate.append(queryTemplate, values, this.collectionFormat);
613+
return QueryTemplate.append(queryTemplate, values, collectionFormat);
595614
}
596615
});
597-
// this.queries.put(name, QueryTemplate.create(name, values));
598616
return this;
599617
}
600618

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ public final class Expressions {
2929

3030
static {
3131
expressions = new LinkedHashMap<>();
32-
expressions.put(Pattern.compile("(\\w[-\\w.]*[ ]*)(:(.+))?"), SimpleExpression.class);
32+
33+
/*
34+
* basic pattern for variable names. this is compliant with RFC 6570 Simple Expressions ONLY
35+
* with the following additional values allowed without required pct-encoding:
36+
*
37+
* - brackets - dashes
38+
*
39+
* see https://tools.ietf.org/html/rfc6570#section-2.3 for more information.
40+
*/
41+
expressions.put(Pattern.compile("(\\w[-\\w.\\[\\]]*[ ]*)(:(.+))?"),
42+
SimpleExpression.class);
3343
}
3444

3545
public static Expression create(final String value, final FragmentType type) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ public void resolveTemplateWithBaseAndParameterizedIterableQuery() {
159159
entry("Queries", asList("us-east-1", "eu-west-1")));
160160
}
161161

162+
@Test
163+
public void resolveTemplateWithMixedCollectionFormatsByQuery() {
164+
RequestTemplate template = new RequestTemplate()
165+
.method(HttpMethod.GET)
166+
.collectionFormat(CollectionFormat.EXPLODED)
167+
.uri("/api/collections")
168+
.query("keys", "{keys}") // default collection format
169+
.query("values[]", Collections.singletonList("{values[]}"), CollectionFormat.CSV);
170+
171+
template = template.resolve(mapOf("keys", Arrays.asList("one", "two"),
172+
"values[]", Arrays.asList("1", "2")));
173+
174+
assertThat(template.url())
175+
.isEqualToIgnoringCase("/api/collections?keys=one&keys=two&values%5B%5D=1,2");
176+
}
177+
162178
@Test
163179
public void resolveTemplateWithHeaderSubstitutions() {
164180
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,16 @@ public void expandSingleValueWithJson() {
164164
assertThat(expanded).isEqualToIgnoringCase(
165165
"json=%7B%22name%22:%22feign%22,%22version%22:%20%2210%22%7D");
166166
}
167+
168+
169+
@Test
170+
public void expandCollectionValueWithBrackets() {
171+
QueryTemplate template =
172+
QueryTemplate.create("collection[]", Collections.singletonList("{collection[]}"),
173+
Util.UTF_8, CollectionFormat.CSV);
174+
String expanded = template.expand(Collections.singletonMap("collection[]",
175+
Arrays.asList("1", "2")));
176+
/* brackets will be pct-encoded */
177+
assertThat(expanded).isEqualToIgnoringCase("collection%5B%5D=1,2");
178+
}
167179
}

0 commit comments

Comments
 (0)