From 65257f229c4c7ca14ead86b0c3d34a3e0fff6aae Mon Sep 17 00:00:00 2001 From: 13wjdgk <13wjdgk@naver.com> Date: Sun, 10 Nov 2024 13:29:13 +0900 Subject: [PATCH] Fix same hashCodes an different queries with include and exclude --- .../data/mongodb/core/query/Field.java | 3 ++- .../data/mongodb/core/query/FieldUnitTests.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java index 6bbdb3ceba..d580988a97 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java @@ -37,6 +37,7 @@ * @author Mark Paluch * @author Owen Q * @author Kirill Egorov + * @author GaEun Kim */ public class Field { @@ -286,7 +287,7 @@ public boolean equals(@Nullable Object o) { @Override public int hashCode() { - int result = ObjectUtils.nullSafeHashCode(criteria); + int result = ObjectUtils.nullSafeHashCode(criteria.toString()); result = 31 * result + ObjectUtils.nullSafeHashCode(slices); result = 31 * result + ObjectUtils.nullSafeHashCode(elemMatches); result = 31 * result + ObjectUtils.nullSafeHashCode(positionKey); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java index 6dba758d87..96f51bd624 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java @@ -28,6 +28,7 @@ * @author Owen Q * @author Mark Paluch * @author Kirill Egorov + * @author GaEun Kim */ class FieldUnitTests { @@ -85,4 +86,18 @@ void overriddenExclusionMethodsCreateEqualFields() { assertThat(left).isEqualTo(right); } + + @Test + void assertDifferentHashCodesForExcludeAndIncludeQueries() { + + Query queryWithExclude = new Query(); + queryWithExclude.fields().exclude("key1"); + queryWithExclude.fields().exclude("key2"); + + Query queryWithInclude = new Query(); + queryWithInclude.fields().include("key1"); + queryWithInclude.fields().include("key2"); + + assertThat(queryWithExclude.hashCode()).isNotEqualTo(queryWithInclude.hashCode()); + } }