Skip to content

Commit e3db282

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

File tree

8 files changed

+68
-47
lines changed

8 files changed

+68
-47
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Default implements Client {
6565

6666
/**
6767
* Disable the request body internal buffering for {@code HttpURLConnection}.
68-
*
68+
*
6969
* @see HttpURLConnection#setFixedLengthStreamingMode(int)
7070
* @see HttpURLConnection#setFixedLengthStreamingMode(long)
7171
* @see HttpURLConnection#setChunkedStreamingMode(int)
@@ -74,7 +74,7 @@ class Default implements Client {
7474

7575
/**
7676
* Create a new client, which disable request buffering by default.
77-
*
77+
*
7878
* @param sslContextFactory SSLSocketFactory for secure https URL connections.
7979
* @param hostnameVerifier the host name verifier.
8080
*/
@@ -86,7 +86,7 @@ public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVeri
8686

8787
/**
8888
* Create a new client.
89-
*
89+
*
9090
* @param sslContextFactory SSLSocketFactory for secure https URL connections.
9191
* @param hostnameVerifier the host name verifier.
9292
* @param disableRequestBuffering Disable the request body internal buffering for
@@ -196,8 +196,19 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
196196
connection.addRequestProperty("Accept", "*/*");
197197
}
198198

199-
if (request.body() != null) {
200-
if (disableRequestBuffering) {
199+
boolean hasEmptyBody = false;
200+
byte[] body = request.body();
201+
if (body == null && request.httpMethod().isWithBody()) {
202+
body = new byte[0];
203+
hasEmptyBody = true;
204+
}
205+
206+
if (body != null) {
207+
/*
208+
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
209+
* retry logic applies to such requests.
210+
*/
211+
if (disableRequestBuffering && !hasEmptyBody) {
201212
if (contentLength != null) {
202213
connection.setFixedLengthStreamingMode(contentLength);
203214
} else {
@@ -212,7 +223,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
212223
out = new DeflaterOutputStream(out);
213224
}
214225
try {
215-
out.write(request.body());
226+
out.write(body);
216227
} finally {
217228
try {
218229
out.close();

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@
3030
public final class Request implements Serializable {
3131

3232
public enum HttpMethod {
33-
GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
33+
GET, HEAD, POST(true), PUT(true), DELETE, CONNECT, OPTIONS, TRACE, PATCH(true);
34+
35+
private final boolean withBody;
36+
37+
HttpMethod() {
38+
this(false);
39+
}
40+
41+
HttpMethod(boolean withBody) {
42+
this.withBody = withBody;
43+
}
44+
45+
public boolean isWithBody() {
46+
return this.withBody;
47+
}
3448
}
3549

3650
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
@@ -203,7 +203,7 @@ protected void log(String configKey, String format, Object... args) {}
203203
}
204204

205205
@Test
206-
public void noResponseBodyForPost() {
206+
public void noResponseBodyForPost() throws Exception {
207207
server.enqueue(new MockResponse());
208208

209209
TestInterface api = newBuilder()
@@ -213,7 +213,7 @@ public void noResponseBodyForPost() {
213213
}
214214

215215
@Test
216-
public void noResponseBodyForPut() {
216+
public void noResponseBodyForPut() throws Exception {
217217
server.enqueue(new MockResponse());
218218

219219
TestInterface api = newBuilder()

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
*/
1414
package feign.client;
1515

16-
import static org.assertj.core.api.Assertions.assertThat;
17-
import static org.hamcrest.core.Is.isA;
18-
import static org.junit.Assert.assertEquals;
19-
import java.io.ByteArrayInputStream;
20-
import java.io.ByteArrayOutputStream;
16+
import feign.Client;
17+
import feign.Client.Proxied;
18+
import feign.Feign;
19+
import feign.Feign.Builder;
20+
import feign.RetryableException;
21+
import feign.assertj.MockWebServerAssertions;
22+
import okhttp3.mockwebserver.MockResponse;
23+
import okhttp3.mockwebserver.SocketPolicy;
24+
import org.junit.Test;
25+
import javax.net.ssl.HostnameVerifier;
26+
import javax.net.ssl.SSLSession;
2127
import java.io.IOException;
2228
import java.net.HttpURLConnection;
2329
import java.net.InetSocketAddress;
@@ -26,20 +32,11 @@
2632
import java.net.Proxy.Type;
2733
import java.net.SocketAddress;
2834
import java.net.URL;
29-
import java.nio.charset.StandardCharsets;
30-
import java.util.zip.DeflaterOutputStream;
31-
import java.util.zip.GZIPOutputStream;
32-
import javax.net.ssl.HostnameVerifier;
33-
import javax.net.ssl.SSLSession;
34-
import okio.Buffer;
35-
import org.junit.Test;
36-
import feign.Client;
37-
import feign.Client.Proxied;
38-
import feign.Feign;
39-
import feign.Feign.Builder;
40-
import feign.RetryableException;
41-
import okhttp3.mockwebserver.MockResponse;
42-
import okhttp3.mockwebserver.SocketPolicy;
35+
import java.util.Collections;
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.entry;
38+
import static org.hamcrest.core.Is.isA;
39+
import static org.junit.Assert.assertEquals;
4340

4441
/**
4542
* Tests client-specific behavior, such as ensuring Content-Length is sent when specified.
@@ -97,6 +94,22 @@ public void testPatch() throws Exception {
9794
super.testPatch();
9895
}
9996

97+
@Override
98+
public void noResponseBodyForPost() throws Exception {
99+
super.noResponseBodyForPost();
100+
MockWebServerAssertions.assertThat(server.takeRequest())
101+
.hasMethod("POST")
102+
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
103+
}
104+
105+
@Override
106+
public void noResponseBodyForPut() throws Exception {
107+
super.noResponseBodyForPut();
108+
MockWebServerAssertions.assertThat(server.takeRequest())
109+
.hasMethod("PUT")
110+
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
111+
}
112+
100113
@Test
101114
@Override
102115
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
@@ -51,7 +51,7 @@ public void testPatch() throws Exception {
5151
}
5252

5353
@Override
54-
public void noResponseBodyForPut() {
54+
public void noResponseBodyForPut() throws Exception {
5555
try {
5656
super.noResponseBodyForPut();
5757
} catch (final IllegalStateException e) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ static Request toOkHttpRequest(feign.Request input) {
7777
}
7878

7979
byte[] inputBody = input.body();
80-
boolean isMethodWithBody =
81-
HttpMethod.POST == input.httpMethod() || HttpMethod.PUT == input.httpMethod()
82-
|| HttpMethod.PATCH == input.httpMethod();
83-
if (isMethodWithBody) {
80+
if (input.httpMethod().isWithBody()) {
8481
requestBuilder.removeHeader("Content-Type");
8582
if (inputBody == null) {
8683
// 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)