use of org.springframework.web.bind.support.WebRequestDataBinder in project spring-framework by spring-projects.
the class ModelAttributeMethodProcessorTests method resolveConstructorListArgumentFromCommaSeparatedRequestParameter.
// gh-25182
@Test
public void resolveConstructorListArgumentFromCommaSeparatedRequestParameter() throws Exception {
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addParameter("listOfStrings", "1,2");
ServletWebRequest requestWithParam = new ServletWebRequest(mockRequest);
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(any(), any(), eq("testBeanWithConstructorArgs"))).willAnswer(invocation -> {
WebRequestDataBinder binder = new WebRequestDataBinder(invocation.getArgument(1));
// Add conversion service which will convert "1,2" to a list
binder.setConversionService(new DefaultFormattingConversionService());
return binder;
});
Object resolved = this.processor.resolveArgument(this.beanWithConstructorArgs, this.container, requestWithParam, factory);
assertThat(resolved).isInstanceOf(TestBeanWithConstructorArgs.class);
assertThat(((TestBeanWithConstructorArgs) resolved).listOfStrings).containsExactly("1", "2");
}
use of org.springframework.web.bind.support.WebRequestDataBinder in project spring-framework by spring-projects.
the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.
// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
WebDataBinder binder = new WebRequestDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
request.addParameter("stringNotAnnot", "");
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
assertThat(arg).isNull();
}
Aggregations