Skip to content

Commit 5055076

Browse files
authored
Merge pull request OpenFeign#3605 from sharanggupta/OpenFeigngh-1210-null-request-body
Treat a null Request.Body as a request with no body
2 parents 301ed2a + 9e80a3b commit 5055076

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### Version 13.17
2+
3+
* `Request` now accepts a `null` `Request.Body` and treats it as "no body", instead of throwing a
4+
`NullPointerException` from `body()`, `charset()`, `isBinary()` and `length()`. A request with no
5+
body reports a `null` body and charset, is binary, and has length 0. Its `toString()` prints the
6+
request line and headers without a body (#1210).
7+
18
### Version 13.16
29

310
* Path-style expansion repeats the entry name for each collection or array value inside a map,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static Request create(
147147
* @param httpMethod for the request.
148148
* @param url for the request.
149149
* @param headers to include.
150-
* @param body of the request, can be {@literal null}
150+
* @param body of the request, can be {@literal null}, meaning the request has no body.
151151
* @return a Request
152152
*/
153153
public static Request create(
@@ -184,7 +184,7 @@ public static Request create(
184184
this.httpMethod = checkNotNull(method, "httpMethod of %s", method.name());
185185
this.url = checkNotNull(url, "url");
186186
this.headers = checkNotNull(headers, "headers of %s %s", method, url);
187-
this.body = body;
187+
this.body = body != null ? body : Body.empty();
188188
this.requestTemplate = requestTemplate;
189189
protocolVersion = ProtocolVersion.HTTP_1_1;
190190
}
@@ -308,7 +308,7 @@ public String toString() {
308308
builder.append(field).append(": ").append(value).append('\n');
309309
}
310310
}
311-
if (body != null) {
311+
if (body() != null) {
312312
builder.append('\n').append(body.asString());
313313
}
314314
return builder.toString();

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import feign.Request.Body;
22+
import feign.Request.HttpMethod;
23+
import java.util.Collections;
2224
import org.junit.jupiter.api.Test;
2325

2426
public class RequestTest {
@@ -34,4 +36,52 @@ void stringBodyIsEncodedWithUtf8NotThePlatformDefault() {
3436
assertThat(body.isBinary()).isFalse();
3537
assertThat(body.asString()).isEqualTo(content);
3638
}
39+
40+
@Test
41+
void requestWithoutBodyHasNullBodyAndCharsetZeroLengthAndIsBinary() {
42+
Request request = createGetRequestWithNullBody();
43+
44+
assertThat(request.body()).isNull();
45+
assertThat(request.charset()).isNull();
46+
assertThat(request.isBinary()).isTrue();
47+
assertThat(request.length()).isZero();
48+
}
49+
50+
@Test
51+
void toStringOfRequestWithoutBodyOmitsBodySection() {
52+
Request request = createGetRequestWithNullBody();
53+
54+
assertThat(request).hasToString("GET http://example.com HTTP/1.1\n");
55+
}
56+
57+
@Test
58+
void requestBuiltFromTemplateWithoutBodyOmitsBodyFromToString() {
59+
Request request =
60+
new RequestTemplate()
61+
.method(HttpMethod.GET)
62+
.target("http://example.com")
63+
.resolve(Collections.emptyMap())
64+
.request();
65+
66+
assertThat(request).hasToString("GET http://example.com HTTP/1.1\n");
67+
}
68+
69+
@Test
70+
void binaryBodyWithoutCharsetPrintsBinaryDataInToString() {
71+
Request request =
72+
new RequestTemplate()
73+
.method(HttpMethod.POST)
74+
.target("http://example.com")
75+
.body(new byte[] {1, 2, 3}, null)
76+
.resolve(Collections.emptyMap())
77+
.request();
78+
79+
assertThat(request)
80+
.hasToString("POST http://example.com HTTP/1.1\nContent-Length: 3\n\nBinary data");
81+
}
82+
83+
private static Request createGetRequestWithNullBody() {
84+
return Request.create(
85+
HttpMethod.GET, "http://example.com", Collections.emptyMap(), (Body) null, null);
86+
}
3787
}

0 commit comments

Comments
 (0)