Leniently accept custom DeferredResult etc subclasses for null values

Issue: SPR-14423
This commit is contained in:
Juergen Hoeller 2016-07-02 12:55:30 +02:00
parent 498d896ef0
commit cfc560c4c4
3 changed files with 63 additions and 29 deletions

View File

@ -283,11 +283,11 @@ public class InvocableHandlerMethod extends HandlerMethod {
if (this.returnValue != null) { if (this.returnValue != null) {
return this.returnValue.getClass(); return this.returnValue.getClass();
} }
if (ResolvableType.NONE.equals(this.returnType)) { if (!ResolvableType.NONE.equals(this.returnType)) {
throw new IllegalArgumentException("Expected Future-like type with generic parameter");
}
return this.returnType.getRawClass(); return this.returnType.getRawClass();
} }
return super.getParameterType();
}
@Override @Override
public Type getGenericParameterType() { public Type getGenericParameterType() {

View File

@ -281,17 +281,11 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
if (this.returnValue != null) { if (this.returnValue != null) {
return this.returnValue.getClass(); return this.returnValue.getClass();
} }
Class<?> parameterType = super.getParameterType(); if (!ResolvableType.NONE.equals(this.returnType)) {
if (ResponseBodyEmitter.class.isAssignableFrom(parameterType) ||
StreamingResponseBody.class.isAssignableFrom(parameterType)) {
return parameterType;
}
if (ResolvableType.NONE.equals(this.returnType)) {
throw new IllegalArgumentException("Expected one of Callable, DeferredResult, or ListenableFuture: " +
super.getParameterType());
}
return this.returnType.getRawClass(); return this.returnType.getRawClass();
} }
return super.getParameterType();
}
@Override @Override
public Type getGenericParameterType() { public Type getGenericParameterType() {

View File

@ -56,6 +56,7 @@ import static org.mockito.Mockito.*;
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen * @author Sam Brannen
* @author Juergen Hoeller
*/ */
public class ServletInvocableHandlerMethodTests { public class ServletInvocableHandlerMethodTests {
@ -126,9 +127,7 @@ public class ServletInvocableHandlerMethodTests {
this.mavContainer.isRequestHandled()); this.mavContainer.isRequestHandled());
} }
// SPR-9159 @Test // SPR-9159
@Test
public void invokeAndHandle_NotVoidWithResponseStatusAndReason() throws Exception { public void invokeAndHandle_NotVoidWithResponseStatusAndReason() throws Exception {
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatusWithReason"); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatusWithReason");
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
@ -169,23 +168,47 @@ public class ServletInvocableHandlerMethodTests {
@Test @Test
public void wrapConcurrentResult_MethodLevelResponseBody() throws Exception { public void wrapConcurrentResult_MethodLevelResponseBody() throws Exception {
wrapConcurrentResult_ResponseBody(new MethodLevelResponseBodyHandler()); wrapConcurrentResult_ResponseBody(new MethodLevelResponseBodyHandler(), "bar", String.class);
}
@Test
public void wrapConcurrentResult_MethodLevelResponseBodyEmpty() throws Exception {
wrapConcurrentResult_ResponseBody(new MethodLevelResponseBodyHandler(), null, String.class);
} }
@Test @Test
public void wrapConcurrentResult_TypeLevelResponseBody() throws Exception { public void wrapConcurrentResult_TypeLevelResponseBody() throws Exception {
wrapConcurrentResult_ResponseBody(new TypeLevelResponseBodyHandler()); wrapConcurrentResult_ResponseBody(new TypeLevelResponseBodyHandler(), "bar", String.class);
} }
private void wrapConcurrentResult_ResponseBody(Object handler) throws Exception { @Test
public void wrapConcurrentResult_TypeLevelResponseBodyEmpty() throws Exception {
wrapConcurrentResult_ResponseBody(new TypeLevelResponseBodyHandler(), null, String.class);
}
@Test
public void wrapConcurrentResult_DeferredResultSubclass() throws Exception {
wrapConcurrentResult_ResponseBody(new DeferredResultSubclassHandler(), "bar", String.class);
}
@Test
public void wrapConcurrentResult_DeferredResultSubclassEmpty() throws Exception {
wrapConcurrentResult_ResponseBody(new DeferredResultSubclassHandler(), null, CustomDeferredResult.class);
}
private void wrapConcurrentResult_ResponseBody(Object handler, Object result, Class<?> expectedReturnType)
throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter()); converters.add(new StringHttpMessageConverter());
this.returnValueHandlers.addHandler(new ModelAndViewMethodReturnValueHandler());
this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters)); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters));
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle"); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle");
handlerMethod = handlerMethod.wrapConcurrentResult("bar");
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
assertEquals("bar", this.response.getContentAsString()); handlerMethod = handlerMethod.wrapConcurrentResult(result);
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
assertEquals((result != null ? result.toString() : ""), this.response.getContentAsString());
assertEquals(expectedReturnType, handlerMethod.getReturnValueType(result).getParameterType());
} }
@Test @Test
@ -200,9 +223,7 @@ public class ServletInvocableHandlerMethodTests {
assertEquals("bar", this.response.getContentAsString()); assertEquals("bar", this.response.getContentAsString());
} }
// SPR-12287 @Test // SPR-12287
@Test
public void wrapConcurrentResult_ResponseEntityNullBody() throws Exception { public void wrapConcurrentResult_ResponseEntityNullBody() throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>(); List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter()); converters.add(new StringHttpMessageConverter());
@ -256,9 +277,7 @@ public class ServletInvocableHandlerMethodTests {
assertEquals("", this.response.getContentAsString()); assertEquals("", this.response.getContentAsString());
} }
// SPR-12287 (16/Oct/14 comments) @Test // SPR-12287 (16/Oct/14 comments)
@Test
public void responseEntityRawTypeWithNullBody() throws Exception { public void responseEntityRawTypeWithNullBody() throws Exception {
List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter()); List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter());
List<Object> advice = Collections.singletonList(mock(ResponseBodyAdvice.class)); List<Object> advice = Collections.singletonList(mock(ResponseBodyAdvice.class));
@ -281,6 +300,7 @@ public class ServletInvocableHandlerMethodTests {
return handlerMethod; return handlerMethod;
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ResponseStatus @ResponseStatus
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@ -290,6 +310,7 @@ public class ServletInvocableHandlerMethodTests {
HttpStatus responseStatus() default HttpStatus.INTERNAL_SERVER_ERROR; HttpStatus responseStatus() default HttpStatus.INTERNAL_SERVER_ERROR;
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class Handler { private static class Handler {
@ -321,6 +342,7 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
private static class ResponseStatusHandler { private static class ResponseStatusHandler {
@ -329,6 +351,7 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
private static class MethodLevelResponseBodyHandler { private static class MethodLevelResponseBodyHandler {
@ResponseBody @ResponseBody
@ -337,6 +360,7 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ResponseBody @ResponseBody
private static class TypeLevelResponseBodyHandler { private static class TypeLevelResponseBodyHandler {
@ -346,6 +370,20 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
private static class DeferredResultSubclassHandler {
@ResponseBody
public CustomDeferredResult handle() {
return new CustomDeferredResult();
}
}
private static class CustomDeferredResult extends DeferredResult<String> {
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class ResponseEntityHandler { private static class ResponseEntityHandler {
@ -358,6 +396,7 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
private static class ExceptionRaisingReturnValueHandler implements HandlerMethodReturnValueHandler { private static class ExceptionRaisingReturnValueHandler implements HandlerMethodReturnValueHandler {
@Override @Override
@ -372,6 +411,7 @@ public class ServletInvocableHandlerMethodTests {
} }
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class AsyncHandler { private static class AsyncHandler {