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 e9c9938016
commit 7a7f7c942d
2 changed files with 22 additions and 2 deletions

View File

@@ -94,6 +94,7 @@ import com.mongodb.DBRef;
* @author Jordi Llach
* @author Mark Paluch
* @author Roman Puchkovskiy
* @author Heesu Jung
*/
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
@@ -1191,7 +1192,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

@@ -80,6 +80,7 @@ import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.BasicDBList;
@@ -95,6 +96,7 @@ import com.mongodb.DBRef;
* @author Christoph Strobl
* @author Mark Paluch
* @author Roman Puchkovskiy
* @author Heesu Jung
*/
@ExtendWith(MockitoExtension.class)
public class MappingMongoConverterUnitTests {
@@ -2159,11 +2161,28 @@ public class MappingMongoConverterUnitTests {
org.bson.Document document = new org.bson.Document("personMap", refMap);
DBRefWrapper result = converter.read(DBRefWrapper.class, document);
verify(afterConvertCallback).onAfterConvert(eq(result.personMap.get("foo")),
eq(new org.bson.Document()), any());
}
@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;
}