Fix Feign ResponseEntity<Void> NPE
fixes gh-539
This commit is contained in:
@@ -39,8 +39,12 @@ public class ResponseEntityDecoder implements Decoder {
|
||||
type = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||
Object decodedObject = decoder.decode(response, type);
|
||||
|
||||
Class<?> clazz = null;
|
||||
if (decodedObject != null) {
|
||||
clazz = decodedObject.getClass();
|
||||
}
|
||||
return createResponse(
|
||||
decodedObject.getClass(),
|
||||
clazz,
|
||||
decodedObject,
|
||||
response);
|
||||
}
|
||||
@@ -56,9 +60,11 @@ public class ResponseEntityDecoder implements Decoder {
|
||||
headers.put(key, new LinkedList<>(response.headers().get(key)));
|
||||
}
|
||||
|
||||
return new ResponseEntity<T>(
|
||||
clazz.cast(instance),
|
||||
headers,
|
||||
T retVal = null;
|
||||
if (clazz != null && instance != null) {
|
||||
retVal = clazz.cast(instance);
|
||||
}
|
||||
return new ResponseEntity<>(retVal, headers,
|
||||
HttpStatus.valueOf(response.status()));
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,17 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
|
||||
assertEquals("first hello didn't match", "hello world 1", hellos.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResponseEntityVoid() {
|
||||
ResponseEntity<Void> response = testClient().getHelloVoid();
|
||||
assertNotNull("response was null", response);
|
||||
List<String> headers = response.getHeaders().get("X-test-header");
|
||||
assertNotNull("headers was null", headers);
|
||||
assertEquals("headers size was wrong", 1, headers.size());
|
||||
String header = headers.get(0);
|
||||
assertEquals("header was wrong", "myval", header);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -114,6 +125,9 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/hellostrings")
|
||||
public List<String> getHelloStrings();
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/hellovoid")
|
||||
public ResponseEntity<Void> getHelloVoid();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -126,6 +140,11 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
|
||||
return ResponseEntity.ok(new Hello("hello world via response"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> getHelloVoid() {
|
||||
return ResponseEntity.noContent().header("X-test-header", "myval").build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hello getHello() {
|
||||
return new Hello("hello world 1");
|
||||
|
||||
Reference in New Issue
Block a user