Skip to content

Commit f5f6bfd

Browse files
committed
Fix same hashCodes an different queries with include and exclude
1 parent 5b1b0a4 commit f5f6bfd

File tree

2 files changed

+21
-1
lines changed
  • spring-data-mongodb/src

2 files changed

+21
-1
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Mark Paluch
3838
* @author Owen Q
3939
* @author Kirill Egorov
40+
* @author GaEun Kim
4041
*/
4142
public class Field {
4243

@@ -286,7 +287,7 @@ public boolean equals(@Nullable Object o) {
286287
@Override
287288
public int hashCode() {
288289

289-
int result = ObjectUtils.nullSafeHashCode(criteria);
290+
int result = ObjectUtils.nullSafeHashCode(criteria.toString());
290291
result = 31 * result + ObjectUtils.nullSafeHashCode(slices);
291292
result = 31 * result + ObjectUtils.nullSafeHashCode(elemMatches);
292293
result = 31 * result + ObjectUtils.nullSafeHashCode(positionKey);

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Owen Q
2929
* @author Mark Paluch
3030
* @author Kirill Egorov
31+
* @author GaEun Kim
3132
*/
3233
class FieldUnitTests {
3334

@@ -85,4 +86,22 @@ void overriddenExclusionMethodsCreateEqualFields() {
8586

8687
assertThat(left).isEqualTo(right);
8788
}
89+
90+
@Test
91+
void assertDifferentHashCodesForExcludeAndIncludeQueries() {
92+
93+
Query queryWithExclude = new Query();
94+
queryWithExclude.fields().exclude("key1");
95+
queryWithExclude.fields().exclude("key2");
96+
queryWithExclude.fields().exclude("field1");
97+
queryWithExclude.fields().exclude("field2");
98+
99+
Query queryWithInclude = new Query();
100+
queryWithInclude.fields().include("key1");
101+
queryWithInclude.fields().include("key2");
102+
queryWithInclude.fields().include("field1");
103+
queryWithInclude.fields().include("field2");
104+
105+
assertThat(queryWithExclude.hashCode()).isNotEqualTo(queryWithInclude.hashCode());
106+
}
88107
}

0 commit comments

Comments
 (0)