From 48cb155f6cd81051ede285555baecb40dd433a79 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 24 Jul 2012 20:13:50 +0200 Subject: [PATCH] DATAMONGO-493 - Fixed broken $ne handling in QueryMapper. $ne expressions are now only being tried to be converted into an ObjectId in case they follow an id property. Previously they tried in every case which might have led to Strings being converted into ObjectIds that accidentally were valid ObjectIds but didn't represent an id at all. --- .../data/mongodb/core/QueryMapper.java | 4 +-- .../core/query/QueryMapperUnitTests.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java index 62305d6e3..5bbee983d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java @@ -86,6 +86,8 @@ public class QueryMapper { ids.add(convertId(id)); } valueDbo.put(inKey, ids.toArray(new Object[ids.size()])); + } else if (valueDbo.containsField("$ne")) { + valueDbo.put("$ne", convertId(valueDbo.get("$ne"))); } else { value = getMappedObject((DBObject) value, null); } @@ -102,8 +104,6 @@ public class QueryMapper { newConditions.add(getMappedObject((DBObject) iter.next(), null)); } value = newConditions; - } else if (key.equals("$ne")) { - value = convertId(value); } newDbo.put(newKey, convertSimpleOrDBObject(value, null)); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java index a461f5339..813541267 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java @@ -21,7 +21,9 @@ 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.ArrayList; import java.util.Arrays; +import java.util.List; import org.bson.types.ObjectId; import org.junit.Before; @@ -211,6 +213,25 @@ public class QueryMapperUnitTests { assertThat(((DBObject) result.get("nested")).get("id"), is(instanceOf(String.class))); } + /** + * @see DATAMONGO-493 + */ + @Test + public void doesNotTranslateNonIdPropertiesFor$NeCriteria() { + + ObjectId accidentallyAnObjectId = new ObjectId(); + + Query query = Query.query(Criteria.where("id").is("id_value").and("publishers") + .ne(accidentallyAnObjectId.toString())); + + DBObject dbObject = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(UserEntity.class)); + assertThat(dbObject.get("publishers"), is(instanceOf(DBObject.class))); + + DBObject publishers = (DBObject) dbObject.get("publishers"); + assertThat(publishers.containsField("$ne"), is(true)); + assertThat(publishers.get("$ne"), is(instanceOf(String.class))); + } + class IdWrapper { Object id; } @@ -236,4 +257,9 @@ public class QueryMapperUnitTests { enum Enum { INSTANCE; } + + class UserEntity { + String id; + List publishers = new ArrayList(); + } }