Fix regression in value to String mapping.
Previous versions allow arbitrary values to be mapped to an string property by calling the ObjectToString converter. This behaviour got lost and is not reestablished. Closes #4371 Original pull request #4373
This commit is contained in:
committed by
Mark Paluch
parent
8803e0383d
commit
a35c4f2717
@@ -2315,8 +2315,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
if (source instanceof Collection) {
|
||||
|
||||
Class<?> rawType = typeHint.getType();
|
||||
if (!Object.class.equals(rawType)) {
|
||||
if (!Object.class.equals(rawType) && !String.class.equals(rawType)) {
|
||||
|
||||
if (!rawType.isArray() && !ClassUtils.isAssignable(Iterable.class, rawType)) {
|
||||
|
||||
throw new MappingException(
|
||||
String.format(INCOMPATIBLE_TYPES, source, source.getClass(), rawType, getPath()));
|
||||
}
|
||||
@@ -2345,11 +2347,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return (S) dbRefConverter.convert(context, (DBRef) source, typeHint);
|
||||
}
|
||||
|
||||
if (source instanceof Collection) {
|
||||
throw new MappingException(
|
||||
String.format(INCOMPATIBLE_TYPES, source, BasicDBList.class, typeHint.getType(), getPath()));
|
||||
}
|
||||
|
||||
if (BsonUtils.supportsBson(source)) {
|
||||
return (S) documentConverter.convert(context, BsonUtils.asBson(source), typeHint);
|
||||
}
|
||||
|
||||
@@ -2831,6 +2831,18 @@ class MappingMongoConverterUnitTests {
|
||||
assertThat(converter.read(Cyclic.class, source).cycle.value).isEqualTo("v2");
|
||||
}
|
||||
|
||||
@Test // GH-4371
|
||||
void shouldConvertTypesToStringTargetType() {
|
||||
|
||||
org.bson.Document source = org.bson.Document.parse("""
|
||||
{
|
||||
city : ["Gotham", "Metropolis"]
|
||||
}
|
||||
""");
|
||||
|
||||
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user