Skip to content

Commit 757b713

Browse files
committed
Use updated message in HttpEntityMethodProcessor
Prior to this commit, the `HttpEntityMethodProcessor` would create a new `ServletServerHttpRequest` input message to parse the native Servlet request, but would not reuse it for reading the request body using the message converters. In gh-32471, we applied a change that updates HTTP headers accordingly when request parameters are read. But not reusing the input message means that we are losing this update when instantiating the resulting `HttpEntity`. This commit ensures that `HttpEntityMethodProcessor` uses the input message it just created when decoding the request body. Fixes gh-36298
1 parent a065563 commit 757b713

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewC
155155
"' in method " + parameter.getMethod() + " is not parameterized");
156156
}
157157

158-
Object body = readWithMessageConverters(webRequest, parameter, paramType);
158+
Object body = readWithMessageConverters(inputMessage, parameter, paramType);
159159
if (RequestEntity.class == parameter.getParameterType()) {
160160
return new RequestEntity<>(body, inputMessage.getHeaders(),
161161
inputMessage.getMethod(), inputMessage.getURI());

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.http.HttpEntity;
3636
import org.springframework.http.HttpHeaders;
3737
import org.springframework.http.MediaType;
38+
import org.springframework.http.RequestEntity;
3839
import org.springframework.http.ResponseEntity;
3940
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
4041
import org.springframework.http.converter.HttpMessageConverter;
@@ -131,6 +132,26 @@ public void resolveArgumentWithEmptyBody() throws Exception {
131132
assertThat(result.getBody()).isNull();
132133
}
133134

135+
@Test // gh-36298
136+
@SuppressWarnings("unchecked")
137+
void shouldResolveEntityWithMutatedHeaders() throws Exception {
138+
servletRequest.setMethod("POST");
139+
servletRequest.addParameter("project", "spring%20framework");
140+
servletRequest.addHeader("Content-Length", "project=spring%20framework".length());
141+
servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
142+
143+
List<HttpMessageConverter<?>> converters = new ArrayList<>();
144+
converters.add(new ByteArrayHttpMessageConverter());
145+
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
146+
147+
Method method = getClass().getDeclaredMethod("resolveAsByteArray", RequestEntity.class);
148+
MethodParameter requestEntity = new MethodParameter(method, 0);
149+
HttpEntity<byte[]> result = (HttpEntity<byte[]>) processor.resolveArgument(requestEntity,
150+
this.mavContainer, this.webRequest, this.binderFactory);
151+
152+
assertThat(result.getBody().length).isEqualTo(result.getHeaders().getContentLength());
153+
}
154+
134155
@Test
135156
void resolveGenericArgument() throws Exception {
136157
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
@@ -269,6 +290,9 @@ private ResponseEntity<CharSequence> handle() {
269290
return null;
270291
}
271292

293+
private void resolveAsByteArray(RequestEntity<byte[]> request) {
294+
}
295+
272296

273297
@SuppressWarnings("unused")
274298
private abstract static class MyParameterizedController<DTO extends Identifiable> {

0 commit comments

Comments
 (0)