Skip to content

Commit 50e3cf5

Browse files
author
adriancole
committed
ensure gson does not attempt to decode void
1 parent 2597d3b commit 50e3cf5

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

gson/src/main/java/feign/gson/GsonModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static class GsonCodec implements Encoder, Decoder {
103103
}
104104

105105
@Override public Object decode(Response response, Type type) throws IOException {
106-
if (response.body() == null) {
106+
if (void.class.equals(type) || response.body() == null) {
107107
return null;
108108
}
109109
Reader reader = response.body().asReader();

gson/src/test/java/feign/gson/GsonModuleTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ static class DecoderBindings {
133133
}.getType()), zones);
134134
}
135135

136+
@Test public void voidDecodesToNull() throws Exception {
137+
DecoderBindings bindings = new DecoderBindings();
138+
ObjectGraph.create(bindings).inject(bindings);
139+
140+
Response response = Response.create(200, "OK", Collections.<String, Collection<String>>emptyMap(), zonesJson);
141+
assertEquals(bindings.decoder.decode(response, void.class), null);
142+
}
143+
144+
@Test public void nullBodyDecodesToNull() throws Exception {
145+
DecoderBindings bindings = new DecoderBindings();
146+
ObjectGraph.create(bindings).inject(bindings);
147+
148+
Response response = Response.create(204, "OK", Collections.<String, Collection<String>>emptyMap(), null);
149+
assertEquals(bindings.decoder.decode(response, String.class), null);
150+
}
151+
136152
private String zonesJson = ""//
137153
+ "[\n"//
138154
+ " {\n"//

0 commit comments

Comments
 (0)