use of cn.taketoday.web.bind.RequestContextDataBinder in project today-infrastructure by TAKETODAY.
the class DataBinderParameterResolver method resolveArgument.
/**
* @return Pojo parameter
*/
@Override
public Object resolveArgument(final RequestContext context, final ResolvableMethodParameter resolvable) throws Throwable {
final Class<?> parameterClass = resolvable.getParameterType();
BeanMetadata beanMetadata = BeanMetadata.from(parameterClass);
Object target = beanMetadata.newInstance();
RequestContextDataBinder dataBinder = new RequestContextDataBinder(target, resolvable.getName());
dataBinder.setConversionService(conversionService);
dataBinder.bind(context);
// #30 Support annotation-supported in the form of DataBinder
resolveAnnotatedProperty(context, resolvable, dataBinder);
// todo dataBinder.validate();
return target;
}
use of cn.taketoday.web.bind.RequestContextDataBinder in project today-framework by TAKETODAY.
the class ModelFactoryTests method updateModelBindingResult.
@Test
public void updateModelBindingResult() throws Throwable {
String commandName = "attr1";
Object command = new Object();
RequestContextDataBinder dataBinder = new RequestContextDataBinder(command, commandName);
BindingContext container = new BindingContext0(dataBinder);
container.addAttribute(commandName, command);
ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
modelFactory.updateModel(this.webRequest, container);
assertThat(container.getModel().get(commandName)).isEqualTo(command);
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
assertThat(container.getModel().get(bindingResultKey)).isSameAs(dataBinder.getBindingResult());
assertThat(container.getModel().size()).isEqualTo(2);
}
use of cn.taketoday.web.bind.RequestContextDataBinder in project today-framework by TAKETODAY.
the class ModelFactoryTests method updateModelWhenRedirecting.
// SPR-12542
@Test
public void updateModelWhenRedirecting() throws Throwable {
String attributeName = "sessionAttr";
String attribute = "value";
RequestContextDataBinder dataBinder = new RequestContextDataBinder(attribute, attributeName);
BindingContext container = new BindingContext0(dataBinder);
container.addAttribute(attributeName, attribute);
String queryParam = "123";
String queryParamName = "q";
container.setRedirectModel(new RedirectModel(queryParamName, queryParam));
ModelFactory modelFactory = new ModelFactory(null, container, this.attributeHandler);
modelFactory.updateModel(this.webRequest, container);
assertThat(container.getRedirectModel().get(queryParamName)).isEqualTo(queryParam);
assertThat(container.getRedirectModel().size()).isEqualTo(1);
assertThat(this.attributeStore.retrieveAttribute(this.webRequest, attributeName)).isEqualTo(attribute);
}
use of cn.taketoday.web.bind.RequestContextDataBinder in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.
// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Throwable {
RequestContextDataBinder binder = new RequestContextDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = mock(BindingContext.class);
given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
given(binderFactory.createBinder(webRequest, "stringNotAnnot")).willReturn(binder);
webRequest.setBindingContext(binderFactory);
request.addParameter("stringNotAnnot", "");
ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
// Object arg = resolver.resolveArgument(webRequest, param);
// assertThat(arg).isNull();
assertThatThrownBy(() -> resolver.resolveArgument(webRequest, param)).isInstanceOf(MissingRequestParameterException.class).hasMessage("Required request parameter 'stringNotAnnot' for method parameter type String is present but converted to null");
}
use of cn.taketoday.web.bind.RequestContextDataBinder in project today-framework by TAKETODAY.
the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueNotRequired.
@Test
public void missingRequestParamEmptyValueNotRequired() throws Throwable {
RequestContextDataBinder binder = new RequestContextDataBinder(null);
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
request.addParameter("name", "");
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
BindingContext binderFactory = mock(BindingContext.class);
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
given(binderFactory.createBinder(webRequest, "name")).willReturn(binder);
webRequest.setBindingContext(binderFactory);
ResolvableMethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
Object arg = resolver.resolveArgument(webRequest, param);
assertThat(arg).isNull();
}
Aggregations