Skip to content

Commit 5c57213

Browse files
dharmeshjogadiavelo
authored andcommitted
Respect decode404 flag and decode 404 response body (OpenFeign#1012)
* decode 404 response body * fix SOAPCodecTest
1 parent a2904ad commit 5c57213

12 files changed

Lines changed: 15 additions & 25 deletions

File tree

gson/src/main/java/feign/gson/GsonDecoder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public GsonDecoder(Gson gson) {
4343

4444
@Override
4545
public Object decode(Response response, Type type) throws IOException {
46-
if (response.status() == 404)
47-
return Util.emptyValueOf(type);
4846
if (response.body() == null)
4947
return null;
5048
Reader reader = response.body().asReader();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ public void customEncoder() {
228228

229229
/** Enabled via {@link feign.Feign.Builder#decode404()} */
230230
@Test
231-
public void notFoundDecodesToEmpty() throws Exception {
231+
public void notFoundDecodesToNull() throws Exception {
232232
Response response = Response.builder()
233233
.status(404)
234234
.reason("NOT FOUND")
235235
.headers(Collections.emptyMap())
236236
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
237237
.build();
238-
assertThat((byte[]) new GsonDecoder().decode(response, byte[].class)).isEmpty();
238+
assertThat((byte[]) new GsonDecoder().decode(response, byte[].class)).isNull();
239239
}
240240
}

jackson-jaxb/src/main/java/feign/jackson/jaxb/JacksonJaxbJsonDecoder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public JacksonJaxbJsonDecoder(ObjectMapper objectMapper) {
3737

3838
@Override
3939
public Object decode(Response response, Type type) throws IOException, FeignException {
40-
if (response.status() == 404)
41-
return Util.emptyValueOf(type);
4240
if (response.body() == null)
4341
return null;
4442
return jacksonJaxbJsonProvider.readFrom(Object.class, type, null, APPLICATION_JSON_TYPE, null,

jackson-jaxb/src/test/java/feign/jackson/jaxb/JacksonJaxbCodecTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public void decodeTest() throws Exception {
5858
* Enabled via {@link feign.Feign.Builder#decode404()}
5959
*/
6060
@Test
61-
public void notFoundDecodesToEmpty() throws Exception {
61+
public void notFoundDecodesToNull() throws Exception {
6262
Response response = Response.builder()
6363
.status(404)
6464
.reason("NOT FOUND")
6565
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
6666
.headers(Collections.emptyMap())
6767
.build();
68-
assertThat((byte[]) new JacksonJaxbJsonDecoder().decode(response, byte[].class)).isEmpty();
68+
assertThat((byte[]) new JacksonJaxbJsonDecoder().decode(response, byte[].class)).isNull();
6969
}
7070

7171
@XmlRootElement

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public JacksonDecoder(ObjectMapper mapper) {
4545

4646
@Override
4747
public Object decode(Response response, Type type) throws IOException {
48-
if (response.status() == 404)
49-
return Util.emptyValueOf(type);
5048
if (response.body() == null)
5149
return null;
5250
Reader reader = response.body().asReader();

jackson/src/main/java/feign/jackson/JacksonIteratorDecoder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public final class JacksonIteratorDecoder implements Decoder {
6666

6767
@Override
6868
public Object decode(Response response, Type type) throws IOException {
69-
if (response.status() == 404)
70-
return Util.emptyValueOf(type);
7169
if (response.body() == null)
7270
return null;
7371
Reader reader = response.body().asReader();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,25 +281,25 @@ public void serialize(Zone value, JsonGenerator jgen, SerializerProvider provide
281281

282282
/** Enabled via {@link feign.Feign.Builder#decode404()} */
283283
@Test
284-
public void notFoundDecodesToEmpty() throws Exception {
284+
public void notFoundDecodesToNull() throws Exception {
285285
Response response = Response.builder()
286286
.status(404)
287287
.reason("NOT FOUND")
288288
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
289289
.headers(Collections.emptyMap())
290290
.build();
291-
assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isEmpty();
291+
assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isNull();
292292
}
293293

294294
/** Enabled via {@link feign.Feign.Builder#decode404()} */
295295
@Test
296-
public void notFoundDecodesToEmptyIterator() throws Exception {
296+
public void notFoundDecodesToNullIterator() throws Exception {
297297
Response response = Response.builder()
298298
.status(404)
299299
.reason("NOT FOUND")
300300
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
301301
.headers(Collections.emptyMap())
302302
.build();
303-
assertThat((byte[]) JacksonIteratorDecoder.create().decode(response, byte[].class)).isEmpty();
303+
assertThat((byte[]) JacksonIteratorDecoder.create().decode(response, byte[].class)).isNull();
304304
}
305305
}

jaxb/src/main/java/feign/jaxb/JAXBDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private JAXBDecoder(Builder builder) {
6464

6565
@Override
6666
public Object decode(Response response, Type type) throws IOException {
67-
if (response.status() == 404 || response.status() == 204)
67+
if (response.status() == 204)
6868
return Util.emptyValueOf(type);
6969
if (response.body() == null)
7070
return null;

jaxb/src/test/java/feign/jaxb/JAXBCodecTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,15 @@ public void decodeAnnotatedParameterizedTypes() throws Exception {
244244
* Enabled via {@link feign.Feign.Builder#decode404()}
245245
*/
246246
@Test
247-
public void notFoundDecodesToEmpty() throws Exception {
247+
public void notFoundDecodesToNull() throws Exception {
248248
Response response = Response.builder()
249249
.status(404)
250250
.reason("NOT FOUND")
251251
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
252252
.headers(Collections.<String, Collection<String>>emptyMap())
253253
.build();
254254
assertThat((byte[]) new JAXBDecoder(new JAXBContextFactory.Builder().build())
255-
.decode(response, byte[].class)).isEmpty();
255+
.decode(response, byte[].class)).isNull();
256256
}
257257

258258
@XmlRootElement

sax/src/main/java/feign/sax/SAXDecoder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public static Builder builder() {
6161

6262
@Override
6363
public Object decode(Response response, Type type) throws IOException, DecodeException {
64-
if (response.status() == 404)
65-
return Util.emptyValueOf(type);
6664
if (response.body() == null)
6765
return null;
6866
ContentHandlerWithResult.Factory<?> handlerFactory = handlerFactories.get(type);

0 commit comments

Comments
 (0)