Skip to content

Commit 8dee7d8

Browse files
committed
Use concrete form-data type when reading request body
Prior to this commit, the WebFlux server support would try reading form-data from the request by: * first, checking that request content-type is compatible with a form-data content-type * then by selecting a message reader that is compatible with the given request content-type This approach is flawed because if the content-type provided by the request is too broad, another message reader could be selected that's not meant to be used for reading form-data. Typically, a JSON message reader could be selected and would fail when reading the request. This problem was previously hidden because message readers would not support `MultiValueMap` as a target type. Now that some readers support this type, this can lead to deserialization errors. This commit now ensures that in all cases, we attempt to read form-data with a message reader that supports the "application/x-www-form-urlencoded" media type. Fixes gh-34660
1 parent 722333f commit 8dee7d8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
153153
return EMPTY_FORM_DATA;
154154
}
155155

156-
HttpMessageReader<MultiValueMap<String, String>> reader = getReader(configurer, contentType, FORM_DATA_TYPE);
156+
HttpMessageReader<MultiValueMap<String, String>> reader = getReader(configurer, MediaType.APPLICATION_FORM_URLENCODED, FORM_DATA_TYPE);
157157
if (reader == null) {
158158
return Mono.error(new IllegalStateException("No HttpMessageReader for " + contentType));
159159
}

spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,10 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.http.HttpHeaders;
22+
import org.springframework.http.MediaType;
2123
import org.springframework.http.codec.ServerCodecConfigurer;
24+
import org.springframework.util.MultiValueMap;
2225
import org.springframework.web.server.ServerWebExchange;
2326
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
2427
import org.springframework.web.server.session.DefaultWebSessionManager;
@@ -56,6 +59,17 @@ void transformUrlWithMultipleEncoders() {
5659
assertThat(exchange.transformUrl("/foo")).isEqualTo("/foo;p=abc?q=123");
5760
}
5861

62+
@Test // gh-34660
63+
void useFormDataMessageReaderWhenAllContentType() {
64+
MockServerHttpRequest request = MockServerHttpRequest
65+
.post("https://example.com")
66+
.header(HttpHeaders.CONTENT_TYPE, MediaType.ALL_VALUE)
67+
.body("project=spring");
68+
ServerWebExchange exchange = createExchange(request);
69+
MultiValueMap<String, String> body = exchange.getFormData().block();
70+
assertThat(body.get("project")).contains("spring");
71+
}
72+
5973

6074
private DefaultServerWebExchange createExchange() {
6175
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();

0 commit comments

Comments
 (0)