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 fb43e85ea5
commit b9af92059d
2 changed files with 11 additions and 6 deletions

View File

@@ -2329,8 +2329,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()));
}
@@ -2359,11 +2361,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

@@ -2858,6 +2858,14 @@ 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("{\n" + " city : [\"Gotham\", \"Metropolis\"]\n" + "}\n");
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
}
static class GenericType<T> {
T content;
}