DATAMONGO-2300 - Add check rawType is null in readMap.

Original Pull Request: #763
This commit is contained in:
Heesu Jung
2019-06-12 14:52:43 +09:00
committed by Christoph Strobl
parent b491d6ce81
commit f4854a15f9
2 changed files with 21 additions and 1 deletions

View File

@@ -76,6 +76,7 @@ import com.mongodb.DBRef;
* @author Christoph Strobl
* @author Jordi Llach
* @author Mark Paluch
* @author Heesu Jung
*/
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
@@ -1120,7 +1121,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
map.put(key, read(defaultedValueType, (BasicDBObject) value, path));
} else if (value instanceof DBRef) {
map.put(key, DBRef.class.equals(rawValueType) ? value
: readAndConvertDBRef((DBRef) value, defaultedValueType, ObjectPath.ROOT, rawValueType));
: readAndConvertDBRef((DBRef) value, defaultedValueType, ObjectPath.ROOT, rawValueType != null ? rawValueType : ClassTypeInformation.OBJECT.getType()));
} else if (value instanceof List) {
map.put(key, readCollectionOrArray(valueType != null ? valueType : ClassTypeInformation.LIST,
(List<Object>) value, path));

View File

@@ -82,6 +82,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.BasicDBList;
@@ -96,6 +97,7 @@ import com.mongodb.DBRef;
* @author Patrik Wasik
* @author Christoph Strobl
* @author Mark Paluch
* @author Heesu Jung
*/
@RunWith(MockitoJUnitRunner.class)
public class MappingMongoConverterUnitTests {
@@ -1938,6 +1940,23 @@ public class MappingMongoConverterUnitTests {
.isEqualTo(new BasicDBObject("property", "value"));
}
@Test // DATAMONGO-2300
public void readAndConvertDBRefNestedByMapCorrectly() {
org.bson.Document cluster = new org.bson.Document("_id", 100L);
DBRef dbRef = new DBRef("clusters", 100L);
org.bson.Document data = new org.bson.Document("_id", 3L);
data.append("cluster", dbRef);
MappingMongoConverter spyConverter = spy(converter);
Mockito.doReturn(cluster).when(spyConverter).readRef(dbRef);
Map<Object, Object> result = spyConverter.readMap(ClassTypeInformation.MAP, data, ObjectPath.ROOT);
assertThat(((LinkedHashMap) result.get("cluster")).get("_id")).isEqualTo(100L);
}
static class GenericType<T> {
T content;
}