From f5f6bfddb7f601e500e370703d624ce39fe87469 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 ++- .../mongodb/core/query/FieldUnitTests.java | 19 +++++++++++++++++++ 2 files changed, 21 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..9fef9540d0 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,22 @@ void overriddenExclusionMethodsCreateEqualFields() { assertThat(left).isEqualTo(right); } + + @Test + void assertDifferentHashCodesForExcludeAndIncludeQueries() { + + Query queryWithExclude = new Query(); + queryWithExclude.fields().exclude("key1"); + queryWithExclude.fields().exclude("key2"); + queryWithExclude.fields().exclude("field1"); + queryWithExclude.fields().exclude("field2"); + + Query queryWithInclude = new Query(); + queryWithInclude.fields().include("key1"); + queryWithInclude.fields().include("key2"); + queryWithInclude.fields().include("field1"); + queryWithInclude.fields().include("field2"); + + assertThat(queryWithExclude.hashCode()).isNotEqualTo(queryWithInclude.hashCode()); + } }