Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to add literals directly to the header #2028

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions core/src/main/java/feign/RequestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ public RequestTemplate header(String name, String... values) {
return header(name, Arrays.asList(values));
}

/**
* @see RequestTemplate#headerLiteral(String, Iterable)
*/
public RequestTemplate headerLiteral(String name, String... values) {
return headerLiteral(name, Arrays.asList(values));
}

/**
* Specify a Header, with the specified values. Values can be literals or template expressions.
*
Expand All @@ -713,16 +720,25 @@ public RequestTemplate header(String name, String... values) {
* @return a RequestTemplate for chaining.
*/
public RequestTemplate header(String name, Iterable<String> values) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is required.");
}
if (values == null) {
values = Collections.emptyList();
}
values = validateHeaderValues(name, values);

return appendHeader(name, values);
}

/**
* Specify a Header, with the specified values. Values will be evaluated as literal.
*
* @param name of the header.
* @param values for this header.
* @return a RequestTemplate for chaining.
*/
public RequestTemplate headerLiteral(String name, Iterable<String> values) {
values = validateHeaderValues(name, values);

return appendHeader(name, values, true);
}


/**
* Clear on reader from {@link RequestTemplate}
*
Expand Down Expand Up @@ -787,6 +803,21 @@ private RequestTemplate appendHeader(String name, Iterable<String> values, boole
return this;
}

/**
* Validates that both the name and values are valid
*
* @param name of the header.
* @param values for this header.
* @return an Iterable of strings
*/
private Iterable<String> validateHeaderValues(String name, Iterable<String> values) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is required.");
}

return values == null ? Collections.emptyList() : values;
}

Comment on lines +806 to +820
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide a private method that will contain the necessary validation for headers

/**
* Headers for this Request.
*
Expand All @@ -802,6 +833,21 @@ public RequestTemplate headers(Map<String, Collection<String>> headers) {
return this;
}

/**
* Header Literals for this Request.
*
* @param headers to use.
* @return a RequestTemplate for chaining.
*/
public RequestTemplate headerLiterals(Map<String, Collection<String>> headers) {
if (headers != null && !headers.isEmpty()) {
headers.forEach(this::headerLiteral);
} else {
this.headers.clear();
}
return this;
}

/**
* Returns an copy of the Headers for this request.
*
Expand Down
13 changes: 12 additions & 1 deletion core/src/test/java/feign/RequestTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static feign.assertj.FeignAssertions.assertThat;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of unused import

import static org.assertj.core.data.MapEntry.entry;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -243,6 +242,18 @@ public void resolveTemplateWithHeaderWithNestedJson() {
.hasHeaders(entry("A-Header", Collections.singletonList(json)));
}

@Test
public void shouldNotResolveTemplateWithHeaderWithJsonLiteral() {
String json = "{ \"foo\": \"bar\", \"hello\": \"world\"}";
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)
.headerLiteral("A-Header", "{aHeader}");

template = template.resolve(mapOf("aHeader", json));

assertThat(template)
.hasHeaders(entry("A-Header", Collections.singletonList("{aHeader}")));
}

Comment on lines +245 to +256
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sure that if a expression that is added as a literal will not be evaluated as an expression when trying to resolve.

/**
* This ensures we don't mess up vnd types
*/
Expand Down