Add headers to data binding values
Closes gh-32676
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.web.reactive;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -26,6 +27,7 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -214,6 +216,14 @@ public class BindingContext {
|
||||
if (!CollectionUtils.isEmpty(vars)) {
|
||||
vars.forEach((key, value) -> addValueIfNotPresent(map, "URI variable", key, value));
|
||||
}
|
||||
HttpHeaders headers = exchange.getRequest().getHeaders();
|
||||
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
List<String> values = entry.getValue();
|
||||
if (!CollectionUtils.isEmpty(values)) {
|
||||
String name = entry.getKey().replace("-", "");
|
||||
addValueIfNotPresent(map, "Header", name, (values.size() == 1 ? values.get(0) : values));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -69,24 +69,50 @@ class BindingContextTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void uriVariablesAddedConditionally() {
|
||||
void bindUriVariablesAndHeaders() {
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest.get("/path")
|
||||
.header("Some-Int-Array", "1")
|
||||
.header("Some-Int-Array", "2")
|
||||
.build();
|
||||
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE,
|
||||
Map.of("name", "John", "age", "25"));
|
||||
|
||||
TestBean target = new TestBean();
|
||||
|
||||
BindingContext bindingContext = new BindingContext(null);
|
||||
WebExchangeDataBinder binder = bindingContext.createDataBinder(exchange, target, "testBean", null);
|
||||
|
||||
binder.bind(exchange).block();
|
||||
|
||||
assertThat(target.getName()).isEqualTo("John");
|
||||
assertThat(target.getAge()).isEqualTo(25);
|
||||
assertThat(target.getSomeIntArray()).containsExactly(1, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bindUriVarsAndHeadersAddedConditionally() {
|
||||
|
||||
MockServerHttpRequest request = MockServerHttpRequest.post("/path")
|
||||
.header("name", "Johnny")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body("name=John&age=25");
|
||||
|
||||
MockServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
exchange.getAttributes().put(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, Map.of("age", "26"));
|
||||
|
||||
TestBean testBean = new TestBean();
|
||||
TestBean target = new TestBean();
|
||||
|
||||
BindingContext bindingContext = new BindingContext(null);
|
||||
WebExchangeDataBinder binder = bindingContext.createDataBinder(exchange, testBean, "testBean", null);
|
||||
WebExchangeDataBinder binder = bindingContext.createDataBinder(exchange, target, "testBean", null);
|
||||
|
||||
binder.bind(exchange).block();
|
||||
|
||||
assertThat(testBean.getName()).isEqualTo("John");
|
||||
assertThat(testBean.getAge()).isEqualTo(25);
|
||||
assertThat(target.getName()).isEqualTo("John");
|
||||
assertThat(target.getAge()).isEqualTo(25);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user