Search in sources :

Example 1 with MethodArgumentNotValidException

use of cn.taketoday.web.bind.MethodArgumentNotValidException 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 MethodArgumentNotValidException

use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-infrastructure by TAKETODAY.

the class ErrorResponseExceptionTests method methodArgumentNotValidException.

@Test
void methodArgumentNotValidException() {
    BindingResult bindingResult = new BindException(new Object(), "object");
    bindingResult.addError(new FieldError("object", "field", "message"));
    ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
    assertStatus(ex, HttpStatus.BAD_REQUEST);
    assertDetail(ex, "Invalid request content.");
    assertThat(ex.getHeaders()).isEmpty();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) BindException(cn.taketoday.validation.BindException) FieldError(cn.taketoday.validation.FieldError) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 3 with MethodArgumentNotValidException

use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.

the class ResponseEntityExceptionHandlerTests method methodArgumentNotValid.

@Test
public void methodArgumentNotValid() throws Exception {
    Exception ex = new MethodArgumentNotValidException(new MethodParameter(getClass().getDeclaredMethod("handle", String.class), 0), new MapBindingResult(Collections.emptyMap(), "name"));
    testException(ex);
}
Also used : MapBindingResult(cn.taketoday.validation.MapBindingResult) MethodParameter(cn.taketoday.core.MethodParameter) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) MissingRequestPartException(cn.taketoday.web.bind.resolver.MissingRequestPartException) MissingPathVariableException(cn.taketoday.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ConversionNotSupportedException(cn.taketoday.beans.ConversionNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) BindException(cn.taketoday.validation.BindException) AsyncRequestTimeoutException(cn.taketoday.web.context.async.AsyncRequestTimeoutException) HttpMessageNotWritableException(cn.taketoday.http.converter.HttpMessageNotWritableException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 4 with MethodArgumentNotValidException

use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.

the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.

@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
    Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
    MethodParameter parameter = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(parameter, bindingResult);
    testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) MapBindingResult(cn.taketoday.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(cn.taketoday.core.MethodParameter) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 5 with MethodArgumentNotValidException

use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.

the class ErrorResponseExceptionTests method methodArgumentNotValidException.

@Test
void methodArgumentNotValidException() {
    BindingResult bindingResult = new BindException(new Object(), "object");
    bindingResult.addError(new FieldError("object", "field", "message"));
    ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
    assertStatus(ex, HttpStatus.BAD_REQUEST);
    assertDetail(ex, "Invalid request content.");
    assertThat(ex.getHeaders()).isEmpty();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) BindException(cn.taketoday.validation.BindException) FieldError(cn.taketoday.validation.FieldError) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Aggregations

MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)6 BindException (cn.taketoday.validation.BindException)5 Test (org.junit.jupiter.api.Test)5 MethodParameter (cn.taketoday.core.MethodParameter)4 BindingResult (cn.taketoday.validation.BindingResult)4 MapBindingResult (cn.taketoday.validation.MapBindingResult)3 ServletException (jakarta.servlet.ServletException)3 FieldError (cn.taketoday.validation.FieldError)2 ObjectError (cn.taketoday.validation.ObjectError)2 Method (java.lang.reflect.Method)2 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)1 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (cn.taketoday.http.converter.HttpMessageNotWritableException)1 Nullable (cn.taketoday.lang.Nullable)1 BindingContext (cn.taketoday.web.BindingContext)1 HttpMediaTypeNotAcceptableException (cn.taketoday.web.HttpMediaTypeNotAcceptableException)1 HttpMediaTypeNotSupportedException (cn.taketoday.web.HttpMediaTypeNotSupportedException)1 HttpRequestMethodNotSupportedException (cn.taketoday.web.HttpRequestMethodNotSupportedException)1 RequestPart (cn.taketoday.web.annotation.RequestPart)1