From f54e1ef880dc3216b498216db6c848934b8ddd84 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 14 Feb 2023 18:12:48 +0000 Subject: [PATCH] Initialize propertyName MethodArgumentTypeMismatchException and MethodArgumentConversionNotSupportedException are TypeMismatchException subclasses with MethodParameter information and should initialize propertyName in TypeMismatchInformation. Closes gh-29959 --- ...ethodArgumentConversionNotSupportedException.java | 3 ++- .../MethodArgumentTypeMismatchException.java | 3 ++- .../RequestHeaderMethodArgumentResolverTests.java | 12 +++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentConversionNotSupportedException.java b/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentConversionNotSupportedException.java index a30602dcdd4..8c070faa3d4 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentConversionNotSupportedException.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentConversionNotSupportedException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ public class MethodArgumentConversionNotSupportedException extends ConversionNot super(value, requiredType, cause); this.name = name; this.parameter = param; + initPropertyName(name); } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentTypeMismatchException.java b/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentTypeMismatchException.java index 2f91434a29c..4f715ff0eb5 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentTypeMismatchException.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/MethodArgumentTypeMismatchException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ public class MethodArgumentTypeMismatchException extends TypeMismatchException { super(value, requiredType, cause); this.name = name; this.parameter = param; + initPropertyName(name); } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java index d03ad39d125..99a7680e7c1 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Unit tests for {@link RequestHeaderMethodArgumentResolver}. @@ -242,15 +243,16 @@ class RequestHeaderMethodArgumentResolverTests { } @Test - void uuidConversionWithInvalidValue() throws Exception { + void uuidConversionWithInvalidValue() { servletRequest.addHeader("name", "bogus-uuid"); ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); bindingInitializer.setConversionService(new DefaultFormattingConversionService()); - assertThatExceptionOfType(MethodArgumentTypeMismatchException.class).isThrownBy( - () -> resolver.resolveArgument(paramUuid, null, webRequest, - new DefaultDataBinderFactory(bindingInitializer))); + assertThatThrownBy(() -> + resolver.resolveArgument(paramUuid, null, webRequest, new DefaultDataBinderFactory(bindingInitializer))) + .isInstanceOf(MethodArgumentTypeMismatchException.class) + .extracting("propertyName").isEqualTo("name"); } @Test