提交 9b0cc00c 编写于 作者: J Jesse Wilson

Merge pull request #666 from square/jw/iterable-headers

Add support for iterable headers.
......@@ -253,11 +253,26 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade {
} else if (annotationType == retrofit.http.Header.class) {
if (value != null) { // Skip null values.
String name = ((retrofit.http.Header) annotation).value();
addHeader(name, value.toString());
if (value instanceof Iterable) {
for (Object iterableValue : (Iterable<?>) value) {
if (iterableValue != null) { // Skip null values.
addHeader(name, iterableValue.toString());
}
}
} else if (value.getClass().isArray()) {
for (int x = 0, arrayLength = Array.getLength(value); x < arrayLength; x++) {
Object arrayValue = Array.get(value, x);
if (arrayValue != null) { // Skip null values.
addHeader(name, arrayValue.toString());
}
}
} else {
addHeader(name, value.toString());
}
}
} else if (annotationType == Field.class) {
String name = ((Field) annotation).value();
if (value != null) { // Skip null values.
String name = ((Field) annotation).value();
if (value instanceof Iterable) {
for (Object iterableValue : (Iterable<?>) value) {
if (iterableValue != null) { // Skip null values.
......@@ -290,8 +305,8 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade {
}
}
} else if (annotationType == Part.class) {
String name = ((Part) annotation).value();
if (value != null) { // Skip null values.
String name = ((Part) annotation).value();
String transferEncoding = ((Part) annotation).encoding();
if (value instanceof TypedOutput) {
multipartBody.addPart(name, transferEncoding, (TypedOutput) value);
......
......@@ -30,7 +30,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* void foo(@Header("Accept-Language") String lang, Callback&lt;Response&gt; cb);
* </pre>
* <p>
* Header parameters may be {@code null} which will omit them from the request.
* Header parameters may be {@code null} which will omit them from the request. Passing a
* {@link java.util.List List} or array will result in a header for each non-{@code null} item.
* <p>
* <strong>Note:</strong> Headers do not overwrite each other. All headers with the same name will
* be included in the request.
......
......@@ -1680,6 +1680,36 @@ public class RequestBuilderTest {
assertThat(request.getBody()).isNull();
}
@Test public void headerParamList() {
class Example {
@GET("/foo/bar/") //
Response method(@retrofit.http.Header("foo") List<String> kit) {
return null;
}
}
Request request = buildRequest(Example.class, Arrays.asList("bar", null, "baz"));
assertThat(request.getMethod()).isEqualTo("GET");
assertThat(request.getHeaders()) //
.containsExactly(new Header("foo", "bar"), new Header("foo", "baz"));
assertThat(request.getUrl()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.getBody()).isNull();
}
@Test public void headerParamArray() {
class Example {
@GET("/foo/bar/") //
Response method(@retrofit.http.Header("foo") String[] kit) {
return null;
}
}
Request request = buildRequest(Example.class, (Object) new String[] { "bar", null, "baz" });
assertThat(request.getMethod()).isEqualTo("GET");
assertThat(request.getHeaders()) //
.containsExactly(new Header("foo", "bar"), new Header("foo", "baz"));
assertThat(request.getUrl()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.getBody()).isNull();
}
@Test public void contentTypeAnnotationHeaderOverrides() {
class Example {
@POST("/") //
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册