DATAMONGO-2043 - Omit type hint when mapping simple types.

Original pull request: #589.
This commit is contained in:
Christoph Strobl
2018-08-06 13:46:39 +02:00
committed by Mark Paluch
parent ff6f5d9ef3
commit e1393847be
2 changed files with 22 additions and 2 deletions

View File

@@ -412,12 +412,23 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
removeFromMap(bson, "_id");
}
boolean handledByCustomConverter = conversions.hasCustomWriteTarget(entityType, Document.class);
if (!handledByCustomConverter && !(bson instanceof Collection)) {
if (requiresTypeHint(entityType)) {
typeMapper.writeType(type, bson);
}
}
/**
* Check if a given type requires a type hint {@literal aka _class attribute} when writing to the document.
*
* @param type must not be {@literal null}.
* @return true if not a simple type, collection or type with custom write target.
*/
private boolean requiresTypeHint(Class<?> type) {
return !conversions.isSimpleType(type) && !ClassUtils.isAssignable(Collection.class, type)
&& !conversions.hasCustomWriteTarget(type, Document.class);
}
/**
* Internal write conversion method which should be used for nested invocations.
*

View File

@@ -1904,6 +1904,15 @@ public class MappingMongoConverterUnitTests {
assertThat(converter.read(Attribute.class, source).value).isInstanceOf(List.class);
}
@Test // DATAMONGO-2043
public void omitsTypeHintWhenWritingSimpleTypes() {
org.bson.Document target = new org.bson.Document();
converter.write(new org.bson.Document("value", "FitzChivalry"), target);
assertThat(target).doesNotContainKeys("_class");
}
static class GenericType<T> {
T content;
}