Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gson/src/main/java/feign/gson/GsonDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -43,7 +43,7 @@ public GsonDecoder(Gson gson) {

@Override
public Object decode(Response response, Type type) throws IOException {
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
Reader reader = response.body().asReader(UTF_8);
try {
Expand Down
16 changes: 15 additions & 1 deletion gson/src/test/java/feign/gson/GsonCodecTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -22,6 +22,7 @@
import feign.Util;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -237,4 +238,17 @@ public void notFoundDecodesToNull() throws Exception {
.build();
assertThat((byte[]) new GsonDecoder().decode(response, byte[].class)).isNull();
}

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) new GsonDecoder().decode(response, byte[].class)).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -36,7 +36,7 @@ public JacksonJaxbJsonDecoder(ObjectMapper objectMapper) {

@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
return jacksonJaxbJsonProvider.readFrom(Object.class, type, null, APPLICATION_JSON_TYPE, null,
response.body().asInputStream());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -20,6 +20,7 @@
import feign.RequestTemplate;
import feign.Response;
import feign.Util;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -69,6 +70,21 @@ public void notFoundDecodesToNull() throws Exception {
assertThat((byte[]) new JacksonJaxbJsonDecoder().decode(response, byte[].class)).isNull();
}

/**
* Enabled via {@link feign.Feign.Builder#decode404()}
*/
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) new JacksonJaxbJsonDecoder().decode(response, byte[].class)).isNull();
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class MockObject {
Expand Down
2 changes: 1 addition & 1 deletion jackson/src/main/java/feign/jackson/JacksonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public JacksonDecoder(ObjectMapper mapper) {

@Override
public Object decode(Response response, Type type) throws IOException {
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
Reader reader = response.body().asReader(response.charset());
if (!reader.markSupported()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -64,7 +64,7 @@ public final class JacksonIteratorDecoder implements Decoder {

@Override
public Object decode(Response response, Type type) throws IOException {
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
Reader reader = response.body().asReader(UTF_8);
if (!reader.markSupported()) {
Expand Down
26 changes: 26 additions & 0 deletions jackson/src/test/java/feign/jackson/JacksonCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ public void notFoundDecodesToNull() throws Exception {
assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isNull();
}

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isNull();
}

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundDecodesToNullIterator() throws Exception {
Expand All @@ -327,4 +340,17 @@ public void notFoundDecodesToNullIterator() throws Exception {
.build();
assertThat((byte[]) JacksonIteratorDecoder.create().decode(response, byte[].class)).isNull();
}

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundWithBodyDecodesToNullIterator() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) JacksonIteratorDecoder.create().decode(response, byte[].class)).isNull();
}
}
4 changes: 2 additions & 2 deletions jaxb/src/main/java/feign/jaxb/JAXBDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -66,7 +66,7 @@ private JAXBDecoder(Builder builder) {
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 204)
return Util.emptyValueOf(type);
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
while (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
Expand Down
19 changes: 18 additions & 1 deletion jaxb/src/test/java/feign/jaxb/JAXBCodecTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -23,6 +23,7 @@
import feign.Util;
import feign.codec.Encoder;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -256,6 +257,22 @@ public void notFoundDecodesToNull() throws Exception {
.decode(response, byte[].class)).isNull();
}

/**
* Enabled via {@link feign.Feign.Builder#decode404()}
*/
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.<String, Collection<String>>emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) new JAXBDecoder(new JAXBContextFactory.Builder().build())
.decode(response, byte[].class)).isNull();
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class MockObject {
Expand Down
4 changes: 2 additions & 2 deletions sax/src/main/java/feign/sax/SAXDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -60,7 +60,7 @@ public static Builder builder() {

@Override
public Object decode(Response response, Type type) throws IOException, DecodeException {
if (response.body() == null)
if (response.status() == 404 || response.body() == null)
return null;
ContentHandlerWithResult.Factory<?> handlerFactory = handlerFactories.get(type);
checkState(handlerFactory != null, "type %s not in configured handlers %s", type,
Expand Down
16 changes: 15 additions & 1 deletion sax/src/test/java/feign/sax/SAXDecoderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -21,6 +21,7 @@
import org.junit.rules.ExpectedException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -105,6 +106,19 @@ public void notFoundDecodesToNull() throws Exception {
assertThat((byte[]) decoder.decode(response, byte[].class)).isNull();
}

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.<String, Collection<String>>emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) decoder.decode(response, byte[].class)).isNull();
}

static enum NetworkStatus {
GOOD, FAILED;
}
Expand Down
16 changes: 16 additions & 0 deletions soap/src/test/java/feign/soap/SOAPCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ public void notFoundDecodesToNull() throws Exception {
.decode(response, byte[].class)).isNull();
}

/**
* Enabled via {@link feign.Feign.Builder#decode404()}
*/
@Test
public void notFoundWithBodyDecodesToNull() throws Exception {
Response response = Response.builder()
.status(404)
.reason("NOT FOUND")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
.headers(Collections.emptyMap())
.body("description explaining the 404", StandardCharsets.UTF_8)
.build();
assertThat((byte[]) new JAXBDecoder(new JAXBContextFactory.Builder().build())
.decode(response, byte[].class)).isNull();
}


@XmlRootElement(name = "GetPrice")
@XmlAccessorType(XmlAccessType.FIELD)
Expand Down