Search in sources :

Example 6 with WebRequestDataBinder

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");
}
Also used : WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 7 with WebRequestDataBinder

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();
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) RequestParam(org.springframework.web.bind.annotation.RequestParam) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) MethodParameter(org.springframework.core.MethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)7 WebDataBinder (org.springframework.web.bind.WebDataBinder)6 Test (org.junit.jupiter.api.Test)5 MethodParameter (org.springframework.core.MethodParameter)3 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 Optional (java.util.Optional)1 Test (org.junit.Test)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1