diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index c5cb84c5c803..1eb90c823bab 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -510,7 +510,7 @@ else if (isMap()) { ObjectUtils.nullSafeEquals(getMapValueTypeDescriptor(), otherDesc.getMapValueTypeDescriptor())); } else { - return true; + return Arrays.equals(getResolvableType().getGenerics(), otherDesc.getResolvableType().getGenerics()); } } diff --git a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index 5fe86254899b..79a8daa99b7b 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java @@ -34,11 +34,13 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import org.junit.jupiter.api.Test; import org.springframework.core.MethodParameter; +import org.springframework.core.ResolvableType; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -663,12 +665,12 @@ void passDownGeneric() throws Exception { } @Test - void upCast() throws Exception { + void upcast() throws Exception { Property property = new Property(getClass(), getClass().getMethod("getProperty"), getClass().getMethod("setProperty", Map.class)); TypeDescriptor typeDescriptor = new TypeDescriptor(property); - TypeDescriptor upCast = typeDescriptor.upcast(Object.class); - assertThat(upCast.getAnnotation(MethodAnnotation1.class)).isNotNull(); + TypeDescriptor upcast = typeDescriptor.upcast(Object.class); + assertThat(upcast.getAnnotation(MethodAnnotation1.class)).isNotNull(); } @Test @@ -682,7 +684,7 @@ void upCastNotSuper() throws Exception { } @Test - void elementTypeForCollectionSubclass() throws Exception { + void elementTypeForCollectionSubclass() { @SuppressWarnings("serial") class CustomSet extends HashSet { } @@ -692,7 +694,7 @@ class CustomSet extends HashSet { } @Test - void elementTypeForMapSubclass() throws Exception { + void elementTypeForMapSubclass() { @SuppressWarnings("serial") class CustomMap extends HashMap { } @@ -704,7 +706,7 @@ class CustomMap extends HashMap { } @Test - void createMapArray() throws Exception { + void createMapArray() { TypeDescriptor mapType = TypeDescriptor.map( LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)); TypeDescriptor arrayType = TypeDescriptor.array(mapType); @@ -713,13 +715,13 @@ void createMapArray() throws Exception { } @Test - void createStringArray() throws Exception { + void createStringArray() { TypeDescriptor arrayType = TypeDescriptor.array(TypeDescriptor.valueOf(String.class)); assertThat(TypeDescriptor.valueOf(String[].class)).isEqualTo(arrayType); } @Test - void createNullArray() throws Exception { + void createNullArray() { assertThat((Object) TypeDescriptor.array(null)).isNull(); } @@ -736,13 +738,13 @@ void serializable() throws Exception { } @Test - void createCollectionWithNullElement() throws Exception { + void createCollectionWithNullElement() { TypeDescriptor typeDescriptor = TypeDescriptor.collection(List.class, null); assertThat(typeDescriptor.getElementTypeDescriptor()).isNull(); } @Test - void createMapWithNullElements() throws Exception { + void createMapWithNullElements() { TypeDescriptor typeDescriptor = TypeDescriptor.map(LinkedHashMap.class, null, null); assertThat(typeDescriptor.getMapKeyTypeDescriptor()).isNull(); assertThat(typeDescriptor.getMapValueTypeDescriptor()).isNull(); @@ -757,6 +759,17 @@ void getSource() throws Exception { assertThat(TypeDescriptor.valueOf(Integer.class).getSource()).isEqualTo(Integer.class); } + @Test // gh-31672 + void equalityWithGenerics() { + ResolvableType rt1 = ResolvableType.forClassWithGenerics(Optional.class, Integer.class); + ResolvableType rt2 = ResolvableType.forClassWithGenerics(Optional.class, String.class); + + TypeDescriptor td1 = new TypeDescriptor(rt1, null, null); + TypeDescriptor td2 = new TypeDescriptor(rt2, null, null); + + assertThat(td1).isNotEqualTo(td2); + } + // Methods designed for test introspection