DATAMONGO-373 - Fixed potential ClassCastException in QueryMapper.

QueryMapper assumed finding a BasicBSONList for $(n)or operators. This is generally true if the DBObject was created through our Query abstraction. If you use the MongoDB driver QueryBuilder this will fail. We're now only insisting on an Iterable which fixes the issue.
This commit is contained in:
Oliver Gierke
2012-01-11 20:06:03 +01:00
parent 817e167056
commit 5ec0cc7327
2 changed files with 14 additions and 2 deletions

View File

@@ -89,9 +89,9 @@ public class QueryMapper {
newKey = "_id";
} else if (key.startsWith("$") && key.endsWith("or")) {
// $or/$nor
BasicBSONList conditions = (BasicBSONList) value;
Iterable<?> conditions = (Iterable<?>) value;
BasicBSONList newConditions = new BasicBSONList();
Iterator<Object> iter = conditions.iterator();
Iterator<?> iter = conditions.iterator();
while (iter.hasNext()) {
newConditions.add(getMappedObject((DBObject) iter.next(), entity));
}

View File

@@ -21,6 +21,7 @@ import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import java.math.BigInteger;
import java.util.Arrays;
import org.bson.types.ObjectId;
import org.junit.Before;
@@ -38,6 +39,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;
/**
* Unit tests for {@link QueryMapper}.
@@ -160,6 +162,16 @@ public class QueryMapperUnitTests {
assertThat(list.get(0).toString(), is(Enum.INSTANCE.name()));
}
/**
* @see DATAMONGO-373
*/
@Test
public void handlesNativelyBuiltQueryCorrectly() {
DBObject query = new QueryBuilder().or(new BasicDBObject("foo", "bar")).get();
mapper.getMappedObject(query, null);
}
class Sample {
@Id