DATADOC-224 - Inspect value entity metadata in case it's a subtype of the declared property.

This commit is contained in:
Oliver Gierke
2011-07-26 23:35:05 +02:00
parent 94f36d27fc
commit 9ed5e6886c
2 changed files with 42 additions and 3 deletions

View File

@@ -440,9 +440,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return;
}
TypeInformation<?> type = prop.getTypeInformation();
if (prop.isMap()) {
BasicDBObject mapDbObj = new BasicDBObject();
writeMapInternal((Map<Object, Object>) obj, mapDbObj, prop.getTypeInformation());
writeMapInternal((Map<Object, Object>) obj, mapDbObj, type);
dbo.put(name, mapDbObj);
return;
}
@@ -464,10 +466,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
}
BasicDBObject propDbObj = new BasicDBObject();
addCustomTypeKeyIfNecessary(prop.getTypeInformation(), obj, propDbObj);
writeInternal(obj, propDbObj, mappingContext.getPersistentEntity(prop.getTypeInformation()));
addCustomTypeKeyIfNecessary(type, obj, propDbObj);
MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass()) ? mappingContext
.getPersistentEntity(obj.getClass()) : mappingContext.getPersistentEntity(type);
writeInternal(obj, propDbObj, entity);
dbo.put(name, propDbObj);
}
private boolean isSubtype(Class<?> left, Class<?> right) {
return left.isAssignableFrom(right) && !left.equals(right);
}
/**
* Returns given object as {@link Collection}. Will return the {@link Collection} as is if the source is a

View File

@@ -465,6 +465,35 @@ public class MappingMongoConverterUnitTests {
assertThat(converter.maybeConvertObject(null), is(nullValue()));
}
@Test
public void writesGenericTypeCorrectly() {
GenericType<Address> type = new GenericType<Address>();
type.content = new Address();
type.content.city = "London";
BasicDBObject result = new BasicDBObject();
converter.write(type, result);
DBObject content = (DBObject) result.get("content");
assertThat(content.get("_class"), is(notNullValue()));
assertThat(content.get("city"), is(notNullValue()));
}
@Test
public void readsGenericTypeCorrectly() {
DBObject address = new BasicDBObject("_class", Address.class.getName());
address.put("city", "London");
GenericType<?> result = converter.read(GenericType.class, new BasicDBObject("content", address));
assertThat(result.content, is(instanceOf(Address.class)));
}
class GenericType<T> {
T content;
}
class ClassWithEnumProperty {