Skip to content

Commit 1f0464e

Browse files
bcrowcsAdrian Cole
authored andcommitted
Fixes end-of-input exception when decoding an empty json body
No content to map due to end-of-input when decoding empty returns using JacksonDecoder. Added UnitTests to show JacksonDecoder error. Added unit test to GsonDecoder to verify behavior is as expected. Added a check for available inputStream data before trying to read into an Object. closes OpenFeign#247
1 parent cd0c108 commit 1f0464e

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

gson/src/test/java/feign/gson/GsonCodecTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ public void nullBodyDecodesToNull() throws Exception {
130130
assertNull(new GsonDecoder().decode(response, String.class));
131131
}
132132

133+
@Test
134+
public void emptyBodyDecodesToNull() throws Exception {
135+
Response response = Response.create(204, "OK",
136+
Collections.<String, Collection<String>>emptyMap(),
137+
new byte[0]);
138+
assertNull(new GsonDecoder().decode(response, String.class));
139+
}
140+
133141
private String zonesJson = ""//
134142
+ "[\n"//
135143
+ " {\n"//

jackson/src/main/java/feign/jackson/JacksonDecoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public Object decode(Response response, Type type) throws IOException {
5252
}
5353
InputStream inputStream = response.body().asInputStream();
5454
try {
55+
if (inputStream.available() <= 0){
56+
return null;
57+
}
5558
return mapper.readValue(inputStream, mapper.constructType(type));
5659
} catch (RuntimeJsonMappingException e) {
5760
if (e.getCause() != null && e.getCause() instanceof IOException) {

jackson/src/test/java/feign/jackson/JacksonCodecTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public void nullBodyDecodesToNull() throws Exception {
9696
assertNull(new JacksonDecoder().decode(response, String.class));
9797
}
9898

99+
@Test
100+
public void emptyBodyDecodesToNull() throws Exception {
101+
Response response = Response.create(204, "OK",
102+
Collections.<String, Collection<String>>emptyMap(),
103+
new byte[0]);
104+
assertNull(new JacksonDecoder().decode(response, String.class));
105+
}
106+
99107
@Test
100108
public void customDecoder() throws Exception {
101109
JacksonDecoder decoder = new JacksonDecoder(

0 commit comments

Comments
 (0)