Add request binding to functional endpoints

This commit introduces form binding to ServerRequest in both WebMVC.fn
and WebFlux.fn's, in the form of a bind(Class) method.

Closes gh-25943
This commit is contained in:
Arjen Poutsma
2023-07-04 15:01:32 +02:00
parent a3e37597aa
commit d04d7b2e57
11 changed files with 598 additions and 7 deletions

View File

@@ -49,6 +49,7 @@ import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
@@ -304,6 +305,71 @@ class DefaultServerRequestTests {
assertThat(request.principal()).contains(principal);
}
@Test
void bindToConstructor() throws BindException {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
servletRequest.addParameter("foo", "FOO");
servletRequest.addParameter("bar", "BAR");
DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
ConstructorInjection result = request.bind(ConstructorInjection.class);
assertThat(result.getFoo()).isEqualTo("FOO");
assertThat(result.getBar()).isEqualTo("BAR");
}
@Test
void bindToProperties() throws BindException {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
servletRequest.addParameter("foo", "FOO");
servletRequest.addParameter("bar", "BAR");
DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
PropertyInjection result = request.bind(PropertyInjection.class);
assertThat(result.getFoo()).isEqualTo("FOO");
assertThat(result.getBar()).isEqualTo("BAR");
}
@Test
void bindToMixed() throws BindException {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
servletRequest.addParameter("foo", "FOO");
servletRequest.addParameter("bar", "BAR");
DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
MixedInjection result = request.bind(MixedInjection.class);
assertThat(result.getFoo()).isEqualTo("FOO");
assertThat(result.getBar()).isEqualTo("BAR");
}
@Test
void bindCustomizer() throws BindException {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
servletRequest.addParameter("foo", "FOO");
servletRequest.addParameter("bar", "BAR");
DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
PropertyInjection result = request.bind(PropertyInjection.class, dataBinder -> dataBinder.setAllowedFields("foo"));
assertThat(result.getFoo()).isEqualTo("FOO");
assertThat(result.getBar()).isNull();
}
@Test
void bindError() throws BindException {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest("GET", "/", true);
servletRequest.addParameter("foo", "FOO");
DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters);
assertThatExceptionOfType(BindException.class).isThrownBy(() ->
request.bind(ErrorInjection.class)
);
}
@ParameterizedHttpMethodTest
void checkNotModifiedTimestamp(String method) throws Exception {
MockHttpServletRequest servletRequest = PathPatternsTestUtils.initRequest(method, "/", true);
@@ -487,4 +553,83 @@ class DefaultServerRequestTests {
@interface ParameterizedHttpMethodTest {
}
private static final class ConstructorInjection {
private final String foo;
private final String bar;
public ConstructorInjection(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}
public String getFoo() {
return this.foo;
}
public String getBar() {
return this.bar;
}
}
private static final class PropertyInjection {
private String foo;
private String bar;
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
private static final class MixedInjection {
private final String foo;
private String bar;
public MixedInjection(String foo) {
this.foo = foo;
}
public String getFoo() {
return this.foo;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
private static final class ErrorInjection {
private int foo;
public int getFoo() {
return this.foo;
}
public void setFoo(int foo) {
this.foo = foo;
}
}
}