Search in sources :

Example 1 with WebDataBinder

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);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) RequestPart(cn.taketoday.web.annotation.RequestPart) MultipartException(cn.taketoday.web.bind.MultipartException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 2 with WebDataBinder

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);
}
Also used : Errors(cn.taketoday.validation.Errors) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MethodParameter(cn.taketoday.core.MethodParameter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with WebDataBinder

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");
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) Test(org.junit.jupiter.api.Test)

Example 4 with WebDataBinder

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();
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) Test(org.junit.jupiter.api.Test)

Example 5 with WebDataBinder

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");
}
Also used : WebDataBinder(cn.taketoday.web.bind.WebDataBinder) Test(org.junit.jupiter.api.Test)

Aggregations

WebDataBinder (cn.taketoday.web.bind.WebDataBinder)13 Test (org.junit.jupiter.api.Test)6 MethodParameter (cn.taketoday.core.MethodParameter)4 BindingContext (cn.taketoday.web.BindingContext)4 Nullable (cn.taketoday.lang.Nullable)3 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)3 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 MethodArgumentConversionNotSupportedException (cn.taketoday.web.handler.method.MethodArgumentConversionNotSupportedException)2 MethodArgumentTypeMismatchException (cn.taketoday.web.handler.method.MethodArgumentTypeMismatchException)2 NamedValueInfo (cn.taketoday.web.handler.method.NamedValueInfo)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 ArrayList (java.util.ArrayList)2 ConversionService (cn.taketoday.core.conversion.ConversionService)1 DefaultFormattingConversionService (cn.taketoday.format.support.DefaultFormattingConversionService)1 Errors (cn.taketoday.validation.Errors)1 LocalValidatorFactoryBean (cn.taketoday.validation.beanvalidation.LocalValidatorFactoryBean)1 RequestContext (cn.taketoday.web.RequestContext)1 RequestPart (cn.taketoday.web.annotation.RequestPart)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1