Consider two org.springframework.data.mongodb.core.query.Query instances almost identical except for one projection field which is
- included in one query
- excluded in the other query
then the following test fails
Assertions.assertNotEquals(queryWithExclude.hashCode(), queryWithInclude.hashCode());
Unit Test
@Test
public void hashMapQueryTest() {
Query queryWithExclude = new Query();
queryWithExclude.fields().exclude("key1");
queryWithExclude.fields().exclude("key2");
Query queryWithInclude = new Query();
queryWithInclude.fields().include("key1");
queryWithInclude.fields().include("key2");
Assertions.assertNotEquals(queryWithExclude.hashCode(), queryWithInclude.hashCode());
}
Comments
The problem can be drilled down to the Field.hashCode()
ObjectUtils.nullSafeHashCode(this.criteria);
which seems to fails due to the HashMap implementation itself as proved by the following test
@Test
public void hashMapZeroOneTest() {
HashMap<String, Integer> fieldZero = new HashMap<>();
fieldZero.put("key1", 0);
fieldZero.put("key2", 0);
HashMap<String, Integer> fieldOne = new HashMap<>();
fieldOne.put("key1", 1);
fieldOne.put("key2", 1);
Assertions.assertNotEquals(fieldZero.hashCode(), fieldOne.hashCode());
}
however tests with other numbers than 0 or 1 works.
To conclude, the problem seem related to the use of 0 and 1 in the Field.criteria which then are translated according to the mongo projection syntax. Possible solution could be to replace in the Field class 0/1 used by include/exclude with enum INCLUDE/EXCLUDE values and consequently modify up to various get[Query|Field]Object().
Comments?
Regards,
Maurizio
Consider two org.springframework.data.mongodb.core.query.Query instances almost identical except for one projection field which is
then the following test fails
Unit Test
Comments
The problem can be drilled down to the Field.hashCode()
which seems to fails due to the HashMap implementation itself as proved by the following test
however tests with other numbers than 0 or 1 works.
To conclude, the problem seem related to the use of 0 and 1 in the Field.criteria which then are translated according to the mongo projection syntax. Possible solution could be to replace in the Field class 0/1 used by include/exclude with enum INCLUDE/EXCLUDE values and consequently modify up to various get[Query|Field]Object().
Comments?
Regards,
Maurizio