Skip to content

Commit 02046da

Browse files
committed
DATACMNS-485 - Fixed bug in ParameterizedTypeInformation.hashCode().
Previously, ParameterizedTypeInformation.hashCode() had been inconsistent to equals in case of a fully resolved parameterized type. This is now fixed by not including the parent reference in case a parameterized type is resolved completely. Also, TypeDiscoverer.getActualType() does not return the component type for non-collection/non-maps anymore to make sure we don't accidentally unwrap parameterized types.
1 parent b3ae206 commit 02046da

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public boolean equals(Object obj) {
181181
*/
182182
@Override
183183
public int hashCode() {
184-
return super.hashCode() + (isResolvedCompletely() ? this.type.hashCode() : 0);
184+
return isResolvedCompletely() ? this.type.hashCode() : super.hashCode();
185185
}
186186

187187
/*

src/main/java/org/springframework/data/util/TypeDiscoverer.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,7 @@ public TypeInformation<?> getActualType() {
284284
return getComponentType();
285285
}
286286

287-
List<TypeInformation<?>> arguments = getTypeArguments();
288-
289-
if (arguments.isEmpty()) {
290-
return this;
291-
}
292-
293-
return arguments.get(arguments.size() - 1);
287+
return this;
294288
}
295289

296290
/*

src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ public void createsToStringRepresentation() {
9595
is("org.springframework.data.util.ParameterizedTypeUnitTests$Localized<java.lang.String>"));
9696
}
9797

98+
/**
99+
* @see DATACMNS-485
100+
*/
101+
@Test
102+
@SuppressWarnings("rawtypes")
103+
public void hashCodeShouldBeConsistentWithEqualsForResolvedTypes() {
104+
105+
TypeInformation first = from(First.class).getProperty("property");
106+
TypeInformation second = from(Second.class).getProperty("property");
107+
108+
assertThat(first, is(second));
109+
assertThat(first.hashCode(), is(second.hashCode()));
110+
}
111+
112+
/**
113+
* @see DATACMNS-485
114+
*/
115+
@Test
116+
@SuppressWarnings("rawtypes")
117+
public void getActualTypeShouldNotUnwrapParameterizedTypes() {
118+
119+
TypeInformation type = from(First.class).getProperty("property");
120+
assertThat(type.getActualType(), is(type));
121+
}
122+
98123
@SuppressWarnings("serial")
99124
class Localized<S> extends HashMap<Locale, S> {
100125
S value;
@@ -109,4 +134,16 @@ class Foo {
109134
Localized<String> param;
110135
Localized2<String> param2;
111136
}
137+
138+
class Parameterized<T> {
139+
T property;
140+
}
141+
142+
class First {
143+
Parameterized<String> property;
144+
}
145+
146+
class Second {
147+
Parameterized<String> property;
148+
}
112149
}

0 commit comments

Comments
 (0)