Browse Source

Add required type to TypeMismatchException message args

Closes gh-35837
pull/35899/head
rstoyanchev 3 weeks ago
parent
commit
4847ee80b0
  1. 2
      framework-docs/modules/ROOT/pages/web/webmvc/mvc-ann-rest-exceptions.adoc
  2. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java
  3. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java

2
framework-docs/modules/ROOT/pages/web/webmvc/mvc-ann-rest-exceptions.adoc

@ -175,7 +175,7 @@ Message codes and arguments for each error are also resolved via `MessageSource`
| `TypeMismatchException` | `TypeMismatchException`
| (default) | (default)
| `+{0}+` property name, `+{1}+` property value | `+{0}+` property name, `+{1}+` property value, `+{2}+` simple name of required type
| `UnsatisfiedServletRequestParameterException` | `UnsatisfiedServletRequestParameterException`
| (default) | (default)

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

@ -466,7 +466,7 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
/** /**
* Customize the handling of {@link TypeMismatchException}. * Customize the handling of {@link TypeMismatchException}.
* <p>By default this method creates a {@link ProblemDetail} with the status * <p>By default, this method creates a {@link ProblemDetail} with the status
* and a short detail message, and also looks up an override for the detail * and a short detail message, and also looks up an override for the detail
* via {@link MessageSource}, before delegating to * via {@link MessageSource}, before delegating to
* {@link #handleExceptionInternal}. * {@link #handleExceptionInternal}.
@ -480,7 +480,8 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
protected @Nullable ResponseEntity<Object> handleTypeMismatch( protected @Nullable ResponseEntity<Object> handleTypeMismatch(
TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) { TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
Object[] args = {ex.getPropertyName(), ex.getValue()}; Object[] args = {ex.getPropertyName(), ex.getValue(),
(ex.getRequiredType() != null ? ex.getRequiredType().getSimpleName() : "")};
String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'"; String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'";
String messageCode = ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null); String messageCode = ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null);
ProblemDetail body = createProblemDetail(ex, status, defaultDetail, messageCode, args, request); ProblemDetail body = createProblemDetail(ex, status, defaultDetail, messageCode, args, request);

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

@ -245,7 +245,7 @@ class ResponseEntityExceptionHandlerTests {
StaticMessageSource messageSource = new StaticMessageSource(); StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage( messageSource.addMessage(
ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null), locale, ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null), locale,
"Failed to set {0} to value: {1}"); "Failed to set {0} to value: {1} for type {2}");
this.exceptionHandler.setMessageSource(messageSource); this.exceptionHandler.setMessageSource(messageSource);
@ -253,7 +253,7 @@ class ResponseEntityExceptionHandlerTests {
new TypeMismatchException(new PropertyChangeEvent(this, "name", "John", "James"), String.class)); new TypeMismatchException(new PropertyChangeEvent(this, "name", "John", "James"), String.class));
ProblemDetail body = (ProblemDetail) entity.getBody(); ProblemDetail body = (ProblemDetail) entity.getBody();
assertThat(body.getDetail()).isEqualTo("Failed to set name to value: James"); assertThat(body.getDetail()).isEqualTo("Failed to set name to value: James for type String");
} }
finally { finally {
LocaleContextHolder.resetLocaleContext(); LocaleContextHolder.resetLocaleContext();

Loading…
Cancel
Save