Skip to content

Commit

Permalink
Raise MethodArgumentNotValidException consistently
Browse files Browse the repository at this point in the history
Closes gh-30100
  • Loading branch information
rstoyanchev committed Mar 14, 2023
1 parent e17f5c5 commit d18bcb3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAn
}
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new BindException(binder.getBindingResult());
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
// Value type adaptation, also covering java.util.Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
Expand Down Expand Up @@ -109,7 +109,7 @@ public void setup() throws Exception {


@Test
public void supportedParameters() throws Exception {
public void supportedParameters() {
assertThat(this.processor.supportsParameter(this.paramNamedValidModelAttr)).isTrue();
assertThat(this.processor.supportsParameter(this.paramModelAttr)).isTrue();

Expand All @@ -119,8 +119,8 @@ public void supportedParameters() throws Exception {
}

@Test
public void supportedParametersInDefaultResolutionMode() throws Exception {
processor = new ModelAttributeMethodProcessor(true);
public void supportedParametersInDefaultResolutionMode() {
this.processor = new ModelAttributeMethodProcessor(true);

// Only non-simple types, even if not annotated
assertThat(this.processor.supportsParameter(this.paramNamedValidModelAttr)).isTrue();
Expand All @@ -132,21 +132,21 @@ public void supportedParametersInDefaultResolutionMode() throws Exception {
}

@Test
public void supportedReturnTypes() throws Exception {
processor = new ModelAttributeMethodProcessor(false);
public void supportedReturnTypes() {
this.processor = new ModelAttributeMethodProcessor(false);
assertThat(this.processor.supportsReturnType(returnParamNamedModelAttr)).isTrue();
assertThat(this.processor.supportsReturnType(returnParamNonSimpleType)).isFalse();
}

@Test
public void supportedReturnTypesInDefaultResolutionMode() throws Exception {
processor = new ModelAttributeMethodProcessor(true);
public void supportedReturnTypesInDefaultResolutionMode() {
this.processor = new ModelAttributeMethodProcessor(true);
assertThat(this.processor.supportsReturnType(returnParamNamedModelAttr)).isTrue();
assertThat(this.processor.supportsReturnType(returnParamNonSimpleType)).isTrue();
}

@Test
public void bindExceptionRequired() throws Exception {
public void bindExceptionRequired() {
assertThat(this.processor.isBindExceptionRequired(null, this.paramNonSimpleType)).isTrue();
assertThat(this.processor.isBindExceptionRequired(null, this.paramNamedValidModelAttr)).isFalse();
}
Expand Down Expand Up @@ -227,11 +227,13 @@ public void resolveArgumentBindException() throws Exception {

StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
dataBinder.getBindingResult().reject("error");

WebDataBinderFactory binderFactory = mock();
given(binderFactory.createBinder(this.request, target, name)).willReturn(dataBinder);

assertThatExceptionOfType(BindException.class).isThrownBy(() ->
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.processor.resolveArgument(this.paramNonSimpleType, this.container, this.request, binderFactory));

verify(binderFactory).createBinder(this.request, target, name);
}

Expand Down

0 comments on commit d18bcb3

Please sign in to comment.