Skip to content

Commit e960636

Browse files
c00lerkdavisk6
andauthored
Set empty request body if it was null (OpenFeign#1778)
Co-authored-by: Kevin Davis <kdavisk6@gmail.com>
1 parent 237dbc6 commit e960636

8 files changed

Lines changed: 54 additions & 29 deletions

File tree

core/src/main/java/feign/Client.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,19 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
199199
connection.addRequestProperty("Accept", "*/*");
200200
}
201201

202-
if (request.body() != null) {
203-
if (disableRequestBuffering) {
202+
boolean hasEmptyBody = false;
203+
byte[] body = request.body();
204+
if (body == null && request.httpMethod().isWithBody()) {
205+
body = new byte[0];
206+
hasEmptyBody = true;
207+
}
208+
209+
if (body != null) {
210+
/*
211+
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
212+
* retry logic applies to such requests.
213+
*/
214+
if (disableRequestBuffering && !hasEmptyBody) {
204215
if (contentLength != null) {
205216
connection.setFixedLengthStreamingMode(contentLength);
206217
} else {
@@ -215,7 +226,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
215226
out = new DeflaterOutputStream(out);
216227
}
217228
try {
218-
out.write(request.body());
229+
out.write(body);
219230
} finally {
220231
try {
221232
out.close();

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,27 @@ public final class Request implements Serializable {
3131
public enum HttpMethod {
3232
GET,
3333
HEAD,
34-
POST,
35-
PUT,
34+
POST(true),
35+
PUT(true),
3636
DELETE,
3737
CONNECT,
3838
OPTIONS,
3939
TRACE,
40-
PATCH
40+
PATCH(true);
41+
42+
private final boolean withBody;
43+
44+
HttpMethod() {
45+
this(false);
46+
}
47+
48+
HttpMethod(boolean withBody) {
49+
this.withBody = withBody;
50+
}
51+
52+
public boolean isWithBody() {
53+
return this.withBody;
54+
}
4155
}
4256

4357
public enum ProtocolVersion {

core/src/test/java/feign/client/AbstractClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected void log(String configKey, String format, Object... args) {}
210210
}
211211

212212
@Test
213-
public void noResponseBodyForPost() {
213+
public void noResponseBodyForPost() throws Exception {
214214
server.enqueue(new MockResponse());
215215

216216
TestInterface api =
@@ -220,7 +220,7 @@ public void noResponseBodyForPost() {
220220
}
221221

222222
@Test
223-
public void noResponseBodyForPut() {
223+
public void noResponseBodyForPut() throws Exception {
224224
server.enqueue(new MockResponse());
225225

226226
TestInterface api =

core/src/test/java/feign/client/DefaultClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package feign.client;
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.entry;
1718
import static org.hamcrest.core.Is.isA;
1819
import static org.junit.Assert.assertEquals;
1920

@@ -22,6 +23,7 @@
2223
import feign.Feign;
2324
import feign.Feign.Builder;
2425
import feign.RetryableException;
26+
import feign.assertj.MockWebServerAssertions;
2527
import java.io.IOException;
2628
import java.net.HttpURLConnection;
2729
import java.net.InetSocketAddress;
@@ -30,6 +32,7 @@
3032
import java.net.Proxy.Type;
3133
import java.net.SocketAddress;
3234
import java.net.URL;
35+
import java.util.Collections;
3336
import javax.net.ssl.HostnameVerifier;
3437
import javax.net.ssl.SSLSession;
3538
import okhttp3.mockwebserver.MockResponse;
@@ -92,6 +95,22 @@ public void testPatch() throws Exception {
9295
super.testPatch();
9396
}
9497

98+
@Override
99+
public void noResponseBodyForPost() throws Exception {
100+
super.noResponseBodyForPost();
101+
MockWebServerAssertions.assertThat(server.takeRequest())
102+
.hasMethod("POST")
103+
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
104+
}
105+
106+
@Override
107+
public void noResponseBodyForPut() throws Exception {
108+
super.noResponseBodyForPut();
109+
MockWebServerAssertions.assertThat(server.takeRequest())
110+
.hasMethod("PUT")
111+
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
112+
}
113+
95114
@Test
96115
@Override
97116
public void noResponseBodyForPatch() {

java11/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@
5151
<scope>test</scope>
5252
</dependency>
5353

54-
<dependency>
55-
<groupId>org.assertj</groupId>
56-
<artifactId>assertj-core</artifactId>
57-
<scope>test</scope>
58-
</dependency>
59-
<dependency>
60-
<groupId>junit</groupId>
61-
<artifactId>junit</artifactId>
62-
<scope>test</scope>
63-
</dependency>
6454
<dependency>
6555
<groupId>io.github.openfeign</groupId>
6656
<artifactId>feign-core</artifactId>

jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void testPatch() throws Exception {
5050
}
5151

5252
@Override
53-
public void noResponseBodyForPut() {
53+
public void noResponseBodyForPut() throws Exception {
5454
try {
5555
super.noResponseBodyForPut();
5656
} catch (final IllegalStateException e) {

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import feign.AsyncClient;
1919
import feign.Client;
20-
import feign.Request.HttpMethod;
2120
import feign.Request.ProtocolVersion;
2221
import java.io.IOException;
2322
import java.io.InputStream;
@@ -78,11 +77,7 @@ static Request toOkHttpRequest(feign.Request input) {
7877
}
7978

8079
byte[] inputBody = input.body();
81-
boolean isMethodWithBody =
82-
HttpMethod.POST == input.httpMethod()
83-
|| HttpMethod.PUT == input.httpMethod()
84-
|| HttpMethod.PATCH == input.httpMethod();
85-
if (isMethodWithBody) {
80+
if (input.httpMethod().isWithBody()) {
8681
requestBuilder.removeHeader("Content-Type");
8782
if (inputBody == null) {
8883
// write an empty BODY to conform with okhttp 2.4.0+

pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,9 @@
517517
</dependencies>
518518
</plugin>
519519

520-
<!-- Ensures checksums are added to published jars -->
521520
<plugin>
522521
<artifactId>maven-install-plugin</artifactId>
523522
<version>${maven-install-plugin.version}</version>
524-
<configuration>
525-
<createChecksum>true</createChecksum>
526-
</configuration>
527523
</plugin>
528524

529525
<plugin>

0 commit comments

Comments
 (0)