Skip to content

Commit

Permalink
Consider generics in equals method (for ConversionService caching)
Browse files Browse the repository at this point in the history
Closes gh-31672
  • Loading branch information
jhoeller committed Nov 24, 2023
1 parent 0599320 commit 710373d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ else if (isMap()) {
ObjectUtils.nullSafeEquals(getMapValueTypeDescriptor(), otherDesc.getMapValueTypeDescriptor()));
}
else {
return true;
return Arrays.equals(getResolvableType().getGenerics(), otherDesc.getResolvableType().getGenerics());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -682,7 +684,7 @@ void upCastNotSuper() throws Exception {
}

@Test
void elementTypeForCollectionSubclass() throws Exception {
void elementTypeForCollectionSubclass() {
@SuppressWarnings("serial")
class CustomSet extends HashSet<String> {
}
Expand All @@ -692,7 +694,7 @@ class CustomSet extends HashSet<String> {
}

@Test
void elementTypeForMapSubclass() throws Exception {
void elementTypeForMapSubclass() {
@SuppressWarnings("serial")
class CustomMap extends HashMap<String, Integer> {
}
Expand All @@ -704,7 +706,7 @@ class CustomMap extends HashMap<String, Integer> {
}

@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);
Expand All @@ -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();
}

Expand All @@ -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();
Expand All @@ -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

Expand Down

0 comments on commit 710373d

Please sign in to comment.