DATADOC-245 - Default to custom target type if raw type is null.
Really only use the custom target type in case it is a real subtype of the basic one. Before that we used the plain custom one which was is lacking generics information potentially available in the basic one.
This commit is contained in:
@@ -851,7 +851,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
Class<?> documentsTargetType = getDefaultedTypeToBeUsed(dbObject);
|
||||
Class<S> rawType = basicType == null ? null : basicType.getType();
|
||||
|
||||
boolean isMoreConcreteCustomType = rawType == null ? false : rawType.isAssignableFrom(documentsTargetType);
|
||||
boolean isMoreConcreteCustomType = rawType == null ? true : rawType.isAssignableFrom(documentsTargetType) && !rawType.equals(documentsTargetType);
|
||||
return isMoreConcreteCustomType ? (TypeInformation<? extends S>) ClassTypeInformation.from(documentsTargetType)
|
||||
: basicType;
|
||||
}
|
||||
|
||||
@@ -624,6 +624,49 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat((String)((Map<?,?>) firstObjectInFoo).get("Hello"), is(equalTo("World")));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see DATADOC-245
|
||||
*/
|
||||
@Test
|
||||
public void readsMapDoublyNestedValuesCorrectly() {
|
||||
|
||||
BasicDBObject nested = new BasicDBObject();
|
||||
BasicDBObject doubly = new BasicDBObject();
|
||||
doubly.append("Hello", "World");
|
||||
nested.append("nested", doubly);
|
||||
DBObject source = new BasicDBObject("mapOfObjects", new BasicDBObject("Foo", nested));
|
||||
|
||||
ClassWithMapProperty result = converter.read(ClassWithMapProperty.class, source);
|
||||
Object foo = result.mapOfObjects.get("Foo");
|
||||
assertThat(foo, is(instanceOf(Map.class)));
|
||||
Object doublyNestedObject = ((Map<?, ?>) foo).get("nested");
|
||||
assertThat(doublyNestedObject, is(instanceOf(Map.class)));
|
||||
assertThat((String) ((Map<?, ?>) doublyNestedObject).get("Hello"), is(equalTo("World")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-245
|
||||
*/
|
||||
@Test
|
||||
public void readsMapListDoublyNestedValuesCorrectly() {
|
||||
|
||||
BasicDBList list = new BasicDBList();
|
||||
BasicDBObject nested = new BasicDBObject();
|
||||
BasicDBObject doubly = new BasicDBObject();
|
||||
doubly.append("Hello", "World");
|
||||
nested.append("nested", doubly);
|
||||
list.add(nested);
|
||||
DBObject source = new BasicDBObject("mapOfObjects", new BasicDBObject("Foo", list));
|
||||
|
||||
ClassWithMapProperty result = converter.read(ClassWithMapProperty.class, source);
|
||||
Object firstObjectInFoo = ((List<?>) result.mapOfObjects.get("Foo")).get(0);
|
||||
assertThat(firstObjectInFoo, is(instanceOf(Map.class)));
|
||||
Object doublyNestedObject = ((Map<?, ?>) firstObjectInFoo).get("nested");
|
||||
assertThat(doublyNestedObject, is(instanceOf(Map.class)));
|
||||
assertThat((String) ((Map<?, ?>) doublyNestedObject).get("Hello"), is(equalTo("World")));
|
||||
}
|
||||
|
||||
class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user