Fix issue with RequestBody(required=true)
Issue: SPR-11828
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -153,41 +153,45 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
|
|||||||
final HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
|
final HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||||
HttpInputMessage inputMessage = new ServletServerHttpRequest(servletRequest);
|
HttpInputMessage inputMessage = new ServletServerHttpRequest(servletRequest);
|
||||||
|
|
||||||
RequestBody annot = methodParam.getParameterAnnotation(RequestBody.class);
|
InputStream inputStream = inputMessage.getBody();
|
||||||
if (!annot.required()) {
|
if (inputStream == null) {
|
||||||
InputStream inputStream = inputMessage.getBody();
|
return handleEmptyBody(methodParam);
|
||||||
if (inputStream == null) {
|
}
|
||||||
return null;
|
else if (inputStream.markSupported()) {
|
||||||
|
inputStream.mark(1);
|
||||||
|
if (inputStream.read() == -1) {
|
||||||
|
return handleEmptyBody(methodParam);
|
||||||
}
|
}
|
||||||
else if (inputStream.markSupported()) {
|
inputStream.reset();
|
||||||
inputStream.mark(1);
|
}
|
||||||
if (inputStream.read() == -1) {
|
else {
|
||||||
return null;
|
final PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
|
||||||
}
|
int b = pushbackInputStream.read();
|
||||||
inputStream.reset();
|
if (b == -1) {
|
||||||
|
return handleEmptyBody(methodParam);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
final PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
|
pushbackInputStream.unread(b);
|
||||||
int b = pushbackInputStream.read();
|
|
||||||
if (b == -1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
pushbackInputStream.unread(b);
|
|
||||||
}
|
|
||||||
inputMessage = new ServletServerHttpRequest(servletRequest) {
|
|
||||||
@Override
|
|
||||||
public InputStream getBody() throws IOException {
|
|
||||||
// Form POST should not get here
|
|
||||||
return pushbackInputStream;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
inputMessage = new ServletServerHttpRequest(servletRequest) {
|
||||||
|
@Override
|
||||||
|
public InputStream getBody() throws IOException {
|
||||||
|
// Form POST should not get here
|
||||||
|
return pushbackInputStream;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.readWithMessageConverters(inputMessage, methodParam, paramType);
|
return super.readWithMessageConverters(inputMessage, methodParam, paramType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object handleEmptyBody(MethodParameter param) {
|
||||||
|
if (param.getParameterAnnotation(RequestBody.class).required()) {
|
||||||
|
throw new HttpMessageNotReadableException("Required request body content is missing: " + param);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleReturnValue(Object returnValue, MethodParameter returnType,
|
public void handleReturnValue(Object returnValue, MethodParameter returnType,
|
||||||
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
|
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.method.annotation;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -32,6 +33,7 @@ import org.springframework.http.HttpInputMessage;
|
|||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||||
@@ -123,7 +125,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||||
|
|
||||||
String body = "Foo";
|
String body = "Foo";
|
||||||
servletRequest.setContent(body.getBytes());
|
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8")));
|
||||||
|
|
||||||
given(messageConverter.canRead(String.class, contentType)).willReturn(true);
|
given(messageConverter.canRead(String.class, contentType)).willReturn(true);
|
||||||
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
|
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
|
||||||
@@ -139,7 +141,8 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
try {
|
try {
|
||||||
testResolveArgumentWithValidation(new SimpleBean(null));
|
testResolveArgumentWithValidation(new SimpleBean(null));
|
||||||
fail("Expected exception");
|
fail("Expected exception");
|
||||||
} catch (MethodArgumentNotValidException e) {
|
}
|
||||||
|
catch (MethodArgumentNotValidException e) {
|
||||||
assertEquals("simpleBean", e.getBindingResult().getObjectName());
|
assertEquals("simpleBean", e.getBindingResult().getObjectName());
|
||||||
assertEquals(1, e.getBindingResult().getErrorCount());
|
assertEquals(1, e.getBindingResult().getErrorCount());
|
||||||
assertNotNull(e.getBindingResult().getFieldError("name"));
|
assertNotNull(e.getBindingResult().getFieldError("name"));
|
||||||
@@ -154,7 +157,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws IOException, Exception {
|
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws IOException, Exception {
|
||||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||||
servletRequest.setContent(new byte[] {});
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
|
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
|
||||||
@@ -170,6 +173,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
public void resolveArgumentCannotRead() throws Exception {
|
public void resolveArgumentCannotRead() throws Exception {
|
||||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||||
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||||
|
|
||||||
given(messageConverter.canRead(String.class, contentType)).willReturn(false);
|
given(messageConverter.canRead(String.class, contentType)).willReturn(false);
|
||||||
|
|
||||||
@@ -178,6 +182,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveArgumentNoContentType() throws Exception {
|
public void resolveArgumentNoContentType() throws Exception {
|
||||||
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||||
given(messageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
given(messageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||||
try {
|
try {
|
||||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||||
@@ -190,14 +195,23 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
|||||||
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
||||||
public void resolveArgumentInvalidContentType() throws Exception {
|
public void resolveArgumentInvalidContentType() throws Exception {
|
||||||
this.servletRequest.setContentType("bad");
|
this.servletRequest.setContentType("bad");
|
||||||
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-9942
|
||||||
|
|
||||||
|
@Test(expected = HttpMessageNotReadableException.class)
|
||||||
|
public void resolveArgumentRequiredNoContent() throws Exception {
|
||||||
|
servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||||
|
servletRequest.setContent(new byte[0]);
|
||||||
|
given(messageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||||
|
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(null);
|
||||||
|
assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveArgumentNotRequiredNoContent() throws Exception {
|
public void resolveArgumentNotRequiredNoContent() throws Exception {
|
||||||
servletRequest.setContent(null);
|
|
||||||
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory()));
|
|
||||||
|
|
||||||
servletRequest.setContent(new byte[0]);
|
servletRequest.setContent(new byte[0]);
|
||||||
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory()));
|
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -34,6 +34,7 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||||
@@ -174,6 +175,18 @@ public class RequestResponseBodyMethodProcessorTests {
|
|||||||
assertEquals("foobarbaz", result);
|
assertEquals("foobarbaz", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-9942
|
||||||
|
|
||||||
|
@Test(expected = HttpMessageNotReadableException.class)
|
||||||
|
public void resolveArgumentRequiredNoContent() throws Exception {
|
||||||
|
this.servletRequest.setContent(new byte[0]);
|
||||||
|
this.servletRequest.setContentType("text/plain");
|
||||||
|
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||||
|
converters.add(new StringHttpMessageConverter());
|
||||||
|
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
|
||||||
|
processor.resolveArgument(paramString, mavContainer, webRequest, binderFactory);
|
||||||
|
}
|
||||||
|
|
||||||
// SPR-9964
|
// SPR-9964
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user