Skip to content

Commit cf31cd1

Browse files
kdavisk6velo
authored andcommitted
Fixes NPE when a Response does not provide headers (#855)
Fixes #853 There are some scenarios reported where a server does not provide headers with the response. While this is not typically expected, it's simple enough for Feign to be resilient to it. This change checks the headers provided in the builder and if none are provided, an empty map is used in it's place.
1 parent 9c5a52d commit cf31cd1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

core/src/main/java/feign/Response.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.charset.Charset;
2323
import java.util.Collection;
2424
import java.util.Collections;
25+
import java.util.LinkedHashMap;
2526
import java.util.LinkedList;
2627
import java.util.Locale;
2728
import java.util.Map;
@@ -49,7 +50,9 @@ private Response(Builder builder) {
4950
this.status = builder.status;
5051
this.request = builder.request;
5152
this.reason = builder.reason; // nullable
52-
this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers));
53+
this.headers = (builder.headers != null)
54+
? Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers))
55+
: new LinkedHashMap<>();
5356
this.body = builder.body; // nullable
5457
}
5558

core/src/test/java/feign/ResponseTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ public void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
7171
Arrays.asList("Cookie-A=Value", "Cookie-B=Value", "Cookie-C=Value");
7272
assertThat(response.headers()).containsOnly(entry(("set-cookie"), expectedHeaderValue));
7373
}
74+
75+
@Test
76+
public void headersAreOptional() {
77+
Response response = Response.builder()
78+
.status(200)
79+
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
80+
.body(new byte[0])
81+
.build();
82+
assertThat(response.headers()).isNotNull().isEmpty();
83+
}
7484
}

0 commit comments

Comments
 (0)