Skip to content

Commit 4847ee8

Browse files
committed
Add required type to TypeMismatchException message args
Closes gh-35837
1 parent f4ee120 commit 4847ee8

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Message codes and arguments for each error are also resolved via `MessageSource`
175175

176176
| `TypeMismatchException`
177177
| (default)
178-
| `+{0}+` property name, `+{1}+` property value
178+
| `+{0}+` property name, `+{1}+` property value, `+{2}+` simple name of required type
179179

180180
| `UnsatisfiedServletRequestParameterException`
181181
| (default)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ else if (ex instanceof AsyncRequestNotUsableException theEx) {
466466

467467
/**
468468
* Customize the handling of {@link TypeMismatchException}.
469-
* <p>By default this method creates a {@link ProblemDetail} with the status
469+
* <p>By default, this method creates a {@link ProblemDetail} with the status
470470
* and a short detail message, and also looks up an override for the detail
471471
* via {@link MessageSource}, before delegating to
472472
* {@link #handleExceptionInternal}.
@@ -480,7 +480,8 @@ else if (ex instanceof AsyncRequestNotUsableException theEx) {
480480
protected @Nullable ResponseEntity<Object> handleTypeMismatch(
481481
TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
482482

483-
Object[] args = {ex.getPropertyName(), ex.getValue()};
483+
Object[] args = {ex.getPropertyName(), ex.getValue(),
484+
(ex.getRequiredType() != null ? ex.getRequiredType().getSimpleName() : "")};
484485
String defaultDetail = "Failed to convert '" + args[0] + "' with value: '" + args[1] + "'";
485486
String messageCode = ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null);
486487
ProblemDetail body = createProblemDetail(ex, status, defaultDetail, messageCode, args, request);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ void typeMismatchWithProblemDetailViaMessageSource() {
245245
StaticMessageSource messageSource = new StaticMessageSource();
246246
messageSource.addMessage(
247247
ErrorResponse.getDefaultDetailMessageCode(TypeMismatchException.class, null), locale,
248-
"Failed to set {0} to value: {1}");
248+
"Failed to set {0} to value: {1} for type {2}");
249249

250250
this.exceptionHandler.setMessageSource(messageSource);
251251

252252
ResponseEntity<?> entity = testException(
253253
new TypeMismatchException(new PropertyChangeEvent(this, "name", "John", "James"), String.class));
254254

255255
ProblemDetail body = (ProblemDetail) entity.getBody();
256-
assertThat(body.getDetail()).isEqualTo("Failed to set name to value: James");
256+
assertThat(body.getDetail()).isEqualTo("Failed to set name to value: James for type String");
257257
}
258258
finally {
259259
LocaleContextHolder.resetLocaleContext();

0 commit comments

Comments
 (0)