mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Merge branch '6.2.x'
This commit is contained in:
+60
-1
@@ -18,11 +18,16 @@ package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import jakarta.servlet.ServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.testfixture.beans.TestBean;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.web.bind.ServletRequestDataBinder;
|
||||
import org.springframework.web.bind.annotation.BindParam;
|
||||
import org.springframework.web.bind.support.BindParamNameResolver;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
|
||||
@@ -45,7 +50,7 @@ class ExtendedServletRequestDataBinderTests {
|
||||
|
||||
|
||||
@Test
|
||||
void createBinder() {
|
||||
void createBinderViaSetters() {
|
||||
request.setAttribute(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE,
|
||||
Map.of("name", "John", "age", "25"));
|
||||
@@ -62,6 +67,27 @@ class ExtendedServletRequestDataBinderTests {
|
||||
assertThat(target.getSomeIntArray()).containsExactly(1, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBinderViaConstructor() {
|
||||
request.setAttribute(
|
||||
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE,
|
||||
Map.of("name", "John", "age", "25"));
|
||||
|
||||
request.addHeader("Some-Int-Array", "1");
|
||||
request.addHeader("Some-Int-Array", "2");
|
||||
|
||||
ServletRequestDataBinder binder = new ExtendedServletRequestDataBinder(null);
|
||||
binder.setTargetType(ResolvableType.forClass(DataBean.class));
|
||||
binder.setNameResolver(new BindParamNameResolver());
|
||||
binder.construct(request);
|
||||
|
||||
DataBean bean = (DataBean) binder.getTarget();
|
||||
|
||||
assertThat(bean.name()).isEqualTo("John");
|
||||
assertThat(bean.age()).isEqualTo(25);
|
||||
assertThat(bean.someIntArray()).containsExactly(1, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uriVarsAndHeadersAddedConditionally() {
|
||||
request.addParameter("name", "John");
|
||||
@@ -78,6 +104,22 @@ class ExtendedServletRequestDataBinderTests {
|
||||
assertThat(target.getAge()).isEqualTo(25);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerPredicate() {
|
||||
TestBinder binder = new TestBinder();
|
||||
binder.addHeaderPredicate(name -> !name.equalsIgnoreCase("Another-Int-Array"));
|
||||
|
||||
MutablePropertyValues mpvs = new MutablePropertyValues();
|
||||
request.addHeader("Priority", "u1");
|
||||
request.addHeader("Some-Int-Array", "1");
|
||||
request.addHeader("Another-Int-Array", "1");
|
||||
|
||||
binder.addBindValues(mpvs, request);
|
||||
|
||||
assertThat(mpvs.size()).isEqualTo(1);
|
||||
assertThat(mpvs.get("someIntArray")).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void noUriTemplateVars() {
|
||||
TestBean target = new TestBean();
|
||||
@@ -88,4 +130,21 @@ class ExtendedServletRequestDataBinderTests {
|
||||
assertThat(target.getAge()).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
||||
private record DataBean(String name, int age, @BindParam("Some-Int-Array") Integer[] someIntArray) {
|
||||
}
|
||||
|
||||
|
||||
private static class TestBinder extends ExtendedServletRequestDataBinder {
|
||||
|
||||
public TestBinder() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
|
||||
super.addBindValues(mpvs, request);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user