use of cn.taketoday.web.bind.WebDataBinder in project today-infrastructure by TAKETODAY.
the class RequestPartMethodArgumentResolver method resolveArgument.
@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
MethodParameter parameter = resolvable.getParameter();
RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
String name = getPartName(parameter, requestPart);
parameter = parameter.nestedIfOptional();
Object arg = null;
Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, context);
if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
arg = mpArg;
} else {
try {
var inputMessage = new RequestPartServletServerHttpRequest(context, name);
arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
BindingContext binderFactory = context.getBindingContext();
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(context, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
binderFactory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
}
} catch (MissingRequestPartException | MultipartException ex) {
if (isRequired) {
throw ex;
}
}
}
if (arg == null && isRequired) {
if (!context.isMultipart()) {
throw new MultipartException("Current request is not a multipart request");
} else {
throw new MissingRequestPartException(name);
}
}
return adaptArgumentIfNecessary(arg, parameter);
}
use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.
the class ErrorsMethodArgumentResolverTests method setup.
@BeforeEach
public void setup() throws Exception {
paramErrors = new ResolvableMethodParameter(new MethodParameter(getClass().getDeclaredMethod("handle", Errors.class), 0));
bindingResult = new WebDataBinder(new Object(), "attr").getBindingResult();
webRequest = new ServletRequestContext(null, new MockHttpServletRequest(), null);
}
use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.
the class InitBinderDataBinderFactoryTests method createBinderWithAttrName.
@Test
public void createBinderWithAttrName() throws Throwable {
InitBinderBindingContext factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
assertThat(dataBinder.getDisallowedFields()).isNotNull();
assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id");
}
use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.
the class InitBinderDataBinderFactoryTests method createBinderNullAttrName.
@Test
public void createBinderNullAttrName() throws Throwable {
InitBinderBindingContext factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
assertThat(dataBinder.getDisallowedFields()).isNull();
}
use of cn.taketoday.web.bind.WebDataBinder in project today-framework by TAKETODAY.
the class InitBinderDataBinderFactoryTests method createBinder.
@Test
public void createBinder() throws Throwable {
InitBinderBindingContext factory = createFactory("initBinder", WebDataBinder.class);
WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
assertThat(dataBinder.getDisallowedFields()).isNotNull();
assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id");
}
Aggregations