DATAMONGO-326 - Fixed enum handling in $in, $nin and $ne criterias.
Updated Criteria implementation to consistently store collections instead of arrays internally.
This commit is contained in:
@@ -76,7 +76,7 @@ public class QueryMapper {
|
||||
if (valueDbo.containsField("$in") || valueDbo.containsField("$nin")) {
|
||||
String inKey = valueDbo.containsField("$in") ? "$in" : "$nin";
|
||||
List<Object> ids = new ArrayList<Object>();
|
||||
for (Object id : (Object[]) valueDbo.get(inKey)) {
|
||||
for (Object id : (Iterable<?>) valueDbo.get(inKey)) {
|
||||
ids.add(convertId(id));
|
||||
}
|
||||
valueDbo.put(inKey, ids.toArray(new Object[ids.size()]));
|
||||
@@ -98,6 +98,9 @@ public class QueryMapper {
|
||||
value = newConditions;
|
||||
} else if (key.equals("$ne")) {
|
||||
value = convertId(value);
|
||||
} else if (value instanceof DBObject) {
|
||||
newDbo.put(newKey, getMappedObject((DBObject) value, entity));
|
||||
return newDbo;
|
||||
}
|
||||
|
||||
newDbo.put(newKey, converter.convertToMongoType(value));
|
||||
|
||||
@@ -170,7 +170,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
throw new InvalidMongoDbApiUsageException("You can only pass in one argument of type "
|
||||
+ o[1].getClass().getName());
|
||||
}
|
||||
criteria.put("$in", o);
|
||||
criteria.put("$in", Arrays.asList(o));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
* @return
|
||||
*/
|
||||
public Criteria in(Collection<?> c) {
|
||||
criteria.put("$in", c.toArray());
|
||||
criteria.put("$in", c);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ package org.springframework.data.mongodb.core.query;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
@@ -127,6 +128,38 @@ public class QueryMapperUnitTests {
|
||||
assertThat(object, is(String.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void handlesEnumsInNotEqualCorrectly() {
|
||||
Query query = query(where("foo").ne(Enum.INSTANCE));
|
||||
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
||||
|
||||
Object object = result.get("foo");
|
||||
assertThat(object, is(DBObject.class));
|
||||
|
||||
Object ne = ((DBObject) object).get("$ne");
|
||||
assertThat(ne, is(String.class));
|
||||
assertThat(ne.toString(), is(Enum.INSTANCE.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesEnumsIn$InCorrectly() {
|
||||
|
||||
Query query = query(where("foo").in(Enum.INSTANCE));
|
||||
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
||||
|
||||
Object object = result.get("foo");
|
||||
assertThat(object, is(DBObject.class));
|
||||
|
||||
Object in = ((DBObject) object).get("$in");
|
||||
assertThat(in, is(BasicDBList.class));
|
||||
|
||||
BasicDBList list = (BasicDBList) in;
|
||||
assertThat(list.size(), is(1));
|
||||
assertThat(list.get(0), is(String.class));
|
||||
assertThat(list.get(0).toString(), is(Enum.INSTANCE.name()));
|
||||
}
|
||||
|
||||
class Sample {
|
||||
|
||||
@Id
|
||||
|
||||
Reference in New Issue
Block a user