Browse Source

Fix id mapping when using `$all` operator.

Fix the id mapping for queries using the $all operator. Prior to this change the collection nature of the id values was not preserved leading to an invalid query.

Original pull request: #4742
Closes #4736
4.3.x
Christoph Strobl 1 year ago committed by Mark Paluch
parent
commit
bfa479108a
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -719,7 +719,7 @@ public class QueryMapper {
for (Entry<String, Object> entry : valueDbo.entrySet()) { for (Entry<String, Object> entry : valueDbo.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
if ("$nin".equals(key) || "$in".equals(key)) { if ("$nin".equals(key) || "$in".equals(key) || "$all".equals(key)) {
List<Object> ids = new ArrayList<>(); List<Object> ids = new ArrayList<>();
for (Object id : (Iterable<?>) valueDbo.get(key)) { for (Object id : (Iterable<?>) valueDbo.get(key)) {
ids.add(convertId(id, getIdTypeForField(documentField))); ids.add(convertId(id, getIdTypeForField(documentField)));

12
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -1684,6 +1684,18 @@ public class QueryMapperUnitTests {
assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $in : [ 'atad' ] } }"); assertThat(mappedObject).isEqualTo("{ 'text' : { $gt : 'gnirps', $in : [ 'atad' ] } }");
} }
@Test // GH-4736
void allOperatorShouldConvertIdCollection() {
ObjectId oid = ObjectId.get();
Criteria criteria = new Criteria().andOperator(where("name").isNull().and("id").all(List.of(oid.toString())));
org.bson.Document mappedObject = mapper.getMappedObject(criteria.getCriteriaObject(),
context.getPersistentEntity(Customer.class));
assertThat(mappedObject).containsEntry("$and.[0]._id.$all", List.of(oid));
}
class WithSimpleMap { class WithSimpleMap {
Map<String, String> simpleMap; Map<String, String> simpleMap;
} }

Loading…
Cancel
Save