DATAMONGO-2410 - Fix Document to BasicDBObject conversion.

Original pull request: #813.
This commit is contained in:
Christoph Strobl
2019-12-03 14:38:19 +01:00
committed by Mark Paluch
parent 05882813ac
commit 11baf455d2
2 changed files with 21 additions and 4 deletions

View File

@@ -211,12 +211,21 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return conversionService.convert(bson, rawType);
}
if (DBObject.class.isAssignableFrom(rawType)) {
if (Document.class.isAssignableFrom(rawType)) {
return (S) bson;
}
if (Document.class.isAssignableFrom(rawType)) {
return (S) bson;
if (DBObject.class.isAssignableFrom(rawType)) {
if (bson instanceof DBObject) {
return (S) bson;
}
if (bson instanceof Document) {
return (S) new BasicDBObject((Document) bson);
}
return (S) DBObject.class.cast(bson);
}
if (typeToUse.isCollectionLike() && bson instanceof List) {
@@ -1648,7 +1657,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
/**
* Returns whether the given type is a sub type of the given reference, i.e. assignable but not the exact same type.
*
*
* @param type must not be {@literal null}.
* @param reference must not be {@literal null}.
* @return

View File

@@ -84,6 +84,7 @@ import org.springframework.data.util.ClassTypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBRef;
/**
@@ -1926,6 +1927,13 @@ public class MappingMongoConverterUnitTests {
assertThat(order.items).hasSize(3);
}
@Test // DATAMONGO-2410
public void shouldAllowReadingBackDbObject() {
assertThat(converter.read(BasicDBObject.class, new org.bson.Document("property", "value")))
.isEqualTo(new BasicDBObject("property", "value"));
}
static class GenericType<T> {
T content;
}