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:
Oliver Gierke
2011-08-26 13:44:47 +02:00
parent fc40e6b08c
commit 95245015bc
2 changed files with 44 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -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;
}