Browse Source

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.
pull/40/merge
Patryk Wąsik 13 years ago committed by Oliver Gierke
parent
commit
23b276745c
  1. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 27
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

@ -41,6 +41,7 @@ import com.mongodb.DBRef;
* *
* @author Jon Brisbin * @author Jon Brisbin
* @author Oliver Gierke * @author Oliver Gierke
* @author Patryk Wasik
*/ */
public class QueryMapper { public class QueryMapper {
@ -267,6 +268,16 @@ public class QueryMapper {
return result; 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); return source == null || source instanceof DBRef ? source : converter.toDBRef(source, property);
} }

27
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -25,6 +25,7 @@ import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.junit.Before; import org.junit.Before;
@ -53,6 +54,7 @@ import com.mongodb.QueryBuilder;
* Unit tests for {@link QueryMapper}. * Unit tests for {@link QueryMapper}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Patryk Wasik
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class QueryMapperUnitTests { public class QueryMapperUnitTests {
@ -363,6 +365,25 @@ public class QueryMapperUnitTests {
assertThat(object.containsField("_id"), is(false)); 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 { class IdWrapper {
Object id; Object id;
} }
@ -415,4 +436,10 @@ public class QueryMapperUnitTests {
WithDBRef withDbRef; WithDBRef withDbRef;
} }
class WithMapDBRef {
@DBRef
Map<String,Sample> mapWithDBRef;
}
} }

Loading…
Cancel
Save