Fix string to objectId mapping error when using query method.
Closes #4490 Original pull request: #4519
This commit is contained in:
committed by
Mark Paluch
parent
f386e05b8f
commit
374ebb801b
@@ -86,6 +86,7 @@ import com.mongodb.DBRef;
|
||||
* @author Mark Paluch
|
||||
* @author David Julia
|
||||
* @author Divya Srivastava
|
||||
* @author Gyungrai Wang
|
||||
*/
|
||||
public class QueryMapper {
|
||||
|
||||
@@ -715,17 +716,20 @@ public class QueryMapper {
|
||||
Document valueDbo = (Document) value;
|
||||
Document resultDbo = new Document(valueDbo);
|
||||
|
||||
if (valueDbo.containsKey("$in") || valueDbo.containsKey("$nin")) {
|
||||
String inKey = valueDbo.containsKey("$in") ? "$in" : "$nin";
|
||||
List<Object> ids = new ArrayList<>();
|
||||
for (Object id : (Iterable<?>) valueDbo.get(inKey)) {
|
||||
ids.add(convertId(id, getIdTypeForField(documentField)));
|
||||
for (Entry<String, Object> entry : valueDbo.entrySet()) {
|
||||
|
||||
String key = entry.getKey();
|
||||
if ("$nin".equals(key) || "$in".equals(key)) {
|
||||
List<Object> ids = new ArrayList<>();
|
||||
for (Object id : (Iterable<?>) valueDbo.get(key)) {
|
||||
ids.add(convertId(id, getIdTypeForField(documentField)));
|
||||
}
|
||||
resultDbo.put(key, ids);
|
||||
} else if (isKeyword(key)) {
|
||||
resultDbo.put(key, convertIdField(documentField, entry.getValue()));
|
||||
} else {
|
||||
resultDbo.put(key, getMappedValue(documentField, entry.getValue()));
|
||||
}
|
||||
resultDbo.put(inKey, ids);
|
||||
} else if (valueDbo.containsKey("$ne")) {
|
||||
resultDbo.put("$ne", convertId(valueDbo.get("$ne"), getIdTypeForField(documentField)));
|
||||
} else {
|
||||
return getMappedObject(resultDbo, Optional.empty());
|
||||
}
|
||||
|
||||
return resultDbo;
|
||||
|
||||
@@ -72,6 +72,7 @@ import com.mongodb.client.model.Filters;
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @author David Julia
|
||||
* @author Gyungrai Wang
|
||||
*/
|
||||
public class QueryMapperUnitTests {
|
||||
|
||||
@@ -129,6 +130,80 @@ public class QueryMapperUnitTests {
|
||||
assertThat(result).containsEntry("_id", id);
|
||||
}
|
||||
|
||||
@Test // GH-4490
|
||||
void translates$GtCorrectly() {
|
||||
|
||||
Criteria criteria = where("id").gt(new ObjectId().toString());
|
||||
|
||||
org.bson.Document query = new org.bson.Document("id", new ObjectId().toString());
|
||||
org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(),
|
||||
context.getPersistentEntity(IdWrapper.class));
|
||||
Object object = result.get("_id");
|
||||
assertThat(object).isInstanceOf(org.bson.Document.class);
|
||||
org.bson.Document document = (org.bson.Document) object;
|
||||
assertThat(document.get("$gt")).isInstanceOf(ObjectId.class);
|
||||
}
|
||||
|
||||
@Test // GH-4490
|
||||
void translates$GteCorrectly() {
|
||||
|
||||
Criteria criteria = where("id").gte(new ObjectId().toString());
|
||||
|
||||
org.bson.Document query = new org.bson.Document("id", new ObjectId().toString());
|
||||
org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(),
|
||||
context.getPersistentEntity(IdWrapper.class));
|
||||
Object object = result.get("_id");
|
||||
assertThat(object).isInstanceOf(org.bson.Document.class);
|
||||
org.bson.Document document = (org.bson.Document) object;
|
||||
assertThat(document.get("$gte")).isInstanceOf(ObjectId.class);
|
||||
}
|
||||
|
||||
@Test // GH-4490
|
||||
void translates$LteCorrectly() {
|
||||
|
||||
Criteria criteria = where("id").lte(new ObjectId().toString());
|
||||
|
||||
org.bson.Document query = new org.bson.Document("id", new ObjectId().toString());
|
||||
org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(),
|
||||
context.getPersistentEntity(IdWrapper.class));
|
||||
Object object = result.get("_id");
|
||||
assertThat(object).isInstanceOf(org.bson.Document.class);
|
||||
org.bson.Document document = (org.bson.Document) object;
|
||||
assertThat(document.get("$lte")).isInstanceOf(ObjectId.class);
|
||||
}
|
||||
|
||||
@Test // GH-4490
|
||||
void translates$LtCorrectly() {
|
||||
|
||||
Criteria criteria = where("id").lt(new ObjectId().toString());
|
||||
|
||||
org.bson.Document query = new org.bson.Document("id", new ObjectId().toString());
|
||||
org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(),
|
||||
context.getPersistentEntity(IdWrapper.class));
|
||||
Object object = result.get("_id");
|
||||
assertThat(object).isInstanceOf(org.bson.Document.class);
|
||||
org.bson.Document document = (org.bson.Document) object;
|
||||
assertThat(document.get("$lt")).isInstanceOf(ObjectId.class);
|
||||
}
|
||||
|
||||
@Test // GH-4490
|
||||
void translatesMultipleCompareOperatorsCorrectly() {
|
||||
|
||||
Criteria criteria = where("id").lt(new ObjectId().toString()).lte(new ObjectId().toString())
|
||||
.gt(new ObjectId().toString()).gte(new ObjectId().toString());
|
||||
|
||||
org.bson.Document query = new org.bson.Document("id", new ObjectId().toString());
|
||||
org.bson.Document result = mapper.getMappedObject(criteria.getCriteriaObject(),
|
||||
context.getPersistentEntity(IdWrapper.class));
|
||||
Object object = result.get("_id");
|
||||
assertThat(object).isInstanceOf(org.bson.Document.class);
|
||||
org.bson.Document document = (org.bson.Document) object;
|
||||
assertThat(document.get("$lt")).isInstanceOf(ObjectId.class);
|
||||
assertThat(document.get("$lte")).isInstanceOf(ObjectId.class);
|
||||
assertThat(document.get("$gt")).isInstanceOf(ObjectId.class);
|
||||
assertThat(document.get("$gte")).isInstanceOf(ObjectId.class);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-278
|
||||
void translates$NeCorrectly() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user