Commit ac86e779 by Spencer Gibb

Fix Feign ResponseEntity<Void> NPE

fixes gh-539
parent 13a3ea07
...@@ -39,8 +39,12 @@ public class ResponseEntityDecoder implements Decoder { ...@@ -39,8 +39,12 @@ public class ResponseEntityDecoder implements Decoder {
type = ((ParameterizedType) type).getActualTypeArguments()[0]; type = ((ParameterizedType) type).getActualTypeArguments()[0];
Object decodedObject = decoder.decode(response, type); Object decodedObject = decoder.decode(response, type);
Class<?> clazz = null;
if (decodedObject != null) {
clazz = decodedObject.getClass();
}
return createResponse( return createResponse(
decodedObject.getClass(), clazz,
decodedObject, decodedObject,
response); response);
} }
...@@ -56,9 +60,11 @@ public class ResponseEntityDecoder implements Decoder { ...@@ -56,9 +60,11 @@ public class ResponseEntityDecoder implements Decoder {
headers.put(key, new LinkedList<>(response.headers().get(key))); headers.put(key, new LinkedList<>(response.headers().get(key)));
} }
return new ResponseEntity<T>( T retVal = null;
clazz.cast(instance), if (clazz != null && instance != null) {
headers, retVal = clazz.cast(instance);
}
return new ResponseEntity<>(retVal, headers,
HttpStatus.valueOf(response.status())); HttpStatus.valueOf(response.status()));
} }
} }
\ No newline at end of file
...@@ -95,6 +95,17 @@ public class SpringDecoderTests extends FeignClientFactoryBean { ...@@ -95,6 +95,17 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
assertEquals("first hello didn't match", "hello world 1", hellos.get(0)); 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 @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
...@@ -114,6 +125,9 @@ public class SpringDecoderTests extends FeignClientFactoryBean { ...@@ -114,6 +125,9 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
@RequestMapping(method = RequestMethod.GET, value = "/hellostrings") @RequestMapping(method = RequestMethod.GET, value = "/hellostrings")
public List<String> getHelloStrings(); public List<String> getHelloStrings();
@RequestMapping(method = RequestMethod.GET, value = "/hellovoid")
public ResponseEntity<Void> getHelloVoid();
} }
@Configuration @Configuration
...@@ -127,6 +141,11 @@ public class SpringDecoderTests extends FeignClientFactoryBean { ...@@ -127,6 +141,11 @@ public class SpringDecoderTests extends FeignClientFactoryBean {
} }
@Override @Override
public ResponseEntity<Void> getHelloVoid() {
return ResponseEntity.noContent().header("X-test-header", "myval").build();
}
@Override
public Hello getHello() { public Hello getHello() {
return new Hello("hello world 1"); return new Hello("hello world 1");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment