Skip to content

Commit ba4d0e3

Browse files
committed
Handle IllegalArgumentException for boolean @RequestParam conversion
1 parent fc1ff88 commit ba4d0e3

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ else if (paramType.isPrimitive()) {
290290
throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(),
291291
namedValueInfo.name, parameter, ex.getCause());
292292
}
293-
catch (TypeMismatchException ex) {
294-
throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(),
295-
namedValueInfo.name, parameter, ex.getCause());
293+
catch (TypeMismatchException | IllegalArgumentException ex) {
294+
throw new MethodArgumentTypeMismatchException(arg, ex instanceof TypeMismatchException tme ?
295+
tme.getRequiredType() : parameterType,
296+
namedValueInfo.name, parameter, ex);
296297
}
297298
return arg;
298299
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,14 @@
4848
import org.springframework.http.server.ServerHttpResponse;
4949
import org.springframework.http.server.ServletServerHttpResponse;
5050
import org.springframework.ui.Model;
51-
import org.springframework.web.bind.annotation.ControllerAdvice;
52-
import org.springframework.web.bind.annotation.ModelAttribute;
53-
import org.springframework.web.bind.annotation.RequestBody;
54-
import org.springframework.web.bind.annotation.RequestParam;
55-
import org.springframework.web.bind.annotation.ResponseBody;
56-
import org.springframework.web.bind.annotation.SessionAttributes;
51+
import org.springframework.web.bind.annotation.*;
5752
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
5853
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest;
5954
import org.springframework.web.context.request.async.WebAsyncManager;
6055
import org.springframework.web.context.request.async.WebAsyncUtils;
6156
import org.springframework.web.context.support.StaticWebApplicationContext;
6257
import org.springframework.web.method.HandlerMethod;
58+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
6359
import org.springframework.web.method.annotation.ModelMethodProcessor;
6460
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
6561
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
@@ -131,6 +127,32 @@ void cacheControlWithoutSessionAttributes() throws Exception {
131127
assertThat(response.getHeader("Cache-Control")).contains("max-age");
132128
}
133129

130+
@Test
131+
void invalidBooleanRequestParamResultsInBadRequest() throws Exception {
132+
this.handlerAdapter.afterPropertiesSet();
133+
134+
this.request.setRequestURI("/test");
135+
this.request.setParameter("bool", "dummy");
136+
137+
HandlerMethod handlerMethod =
138+
handlerMethod(new BooleanParamController(), "test", boolean.class);
139+
140+
assertThatThrownBy(() -> {
141+
try {
142+
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
143+
}
144+
catch (Exception ex) {
145+
throw new RuntimeException(ex);
146+
}
147+
})
148+
.hasCauseInstanceOf(MethodArgumentTypeMismatchException.class);
149+
150+
}
151+
152+
153+
154+
155+
134156
@Test
135157
void cacheControlWithSessionAttributes() throws Exception {
136158
SessionAttributeController handler = new SessionAttributeController();
@@ -410,6 +432,15 @@ public ResponseEntity<?> handle(@RequestParam String q) throws IOException {
410432

411433
}
412434

435+
@RestController
436+
static class BooleanParamController {
437+
438+
@GetMapping("/test")
439+
void test(@RequestParam boolean bool) {
440+
}
441+
}
442+
443+
413444

414445
@ControllerAdvice
415446
private static class ModelAttributeAdvice {

0 commit comments

Comments
 (0)