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:
Christoph Strobl
2023-04-25 10:03:16 +02:00
committed by Mark Paluch
parent 8803e0383d
commit a35c4f2717
2 changed files with 15 additions and 6 deletions

View File

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

View File

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