DATAMONGO-677 - QueryMapper now handles DBRefs in Maps correctly.

QueryMapper now handle Map with DBRef value, which is needed to process an update in MongoTemplate.doUpdate(…) to save versioned document correctly.
This commit is contained in:
Patryk Wąsik
2013-05-21 15:58:46 +02:00
committed by Oliver Gierke
parent be0092d3f5
commit 23b276745c
2 changed files with 38 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ import com.mongodb.DBRef;
*
* @author Jon Brisbin
* @author Oliver Gierke
* @author Patryk Wasik
*/
public class QueryMapper {
@@ -267,6 +268,16 @@ public class QueryMapper {
return result;
}
if (property.isMap()) {
BasicDBObject result = new BasicDBObject();
DBObject dbObject = (DBObject) source;
for (String key : dbObject.keySet()) {
Object o = dbObject.get(key);
result.put(key, o instanceof DBRef ? o : converter.toDBRef(o, property));
}
return result;
}
return source == null || source instanceof DBRef ? source : converter.toDBRef(source, property);
}

View File

@@ -25,6 +25,7 @@ import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.bson.types.ObjectId;
import org.junit.Before;
@@ -53,6 +54,7 @@ import com.mongodb.QueryBuilder;
* Unit tests for {@link QueryMapper}.
*
* @author Oliver Gierke
* @author Patryk Wasik
*/
@RunWith(MockitoJUnitRunner.class)
public class QueryMapperUnitTests {
@@ -363,6 +365,25 @@ public class QueryMapperUnitTests {
assertThat(object.containsField("_id"), is(false));
}
/**
* @see DATAMONGO-677
*/
@Test
public void handleMapWithDBRefCorrectly() {
DBObject mapDbObject = new BasicDBObject();
mapDbObject.put("test", new com.mongodb.DBRef(null, "test", "test"));
DBObject dbObject = new BasicDBObject();
dbObject.put("mapWithDBRef", mapDbObject);
DBObject mapped = mapper.getMappedObject(dbObject, context.getPersistentEntity(WithMapDBRef.class));
assertThat(mapped.containsField("mapWithDBRef"), is(true));
assertThat(mapped.get("mapWithDBRef"), instanceOf(BasicDBObject.class));
assertThat(((BasicDBObject) mapped.get("mapWithDBRef")).containsField("test"), is(true));
assertThat(((BasicDBObject) mapped.get("mapWithDBRef")).get("test"), instanceOf(com.mongodb.DBRef.class));
}
class IdWrapper {
Object id;
}
@@ -415,4 +436,10 @@ public class QueryMapperUnitTests {
WithDBRef withDbRef;
}
class WithMapDBRef {
@DBRef
Map<String,Sample> mapWithDBRef;
}
}