Skip to content

Commit 289438b

Browse files
christophstroblmp911de
authored andcommitted
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
1 parent 83958ba commit 289438b

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,8 +2313,10 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23132313
if (source instanceof Collection<?> collection) {
23142314

23152315
Class<?> rawType = typeHint.getType();
2316-
if (!Object.class.equals(rawType)) {
2316+
if (!Object.class.equals(rawType) && !String.class.equals(rawType)) {
2317+
23172318
if (!rawType.isArray() && !ClassUtils.isAssignable(Iterable.class, rawType)) {
2319+
23182320
throw new MappingException(
23192321
String.format(INCOMPATIBLE_TYPES, source, source.getClass(), rawType, getPath()));
23202322
}
@@ -2343,11 +2345,6 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23432345
return (S) dbRefConverter.convert(context, dbRef, typeHint);
23442346
}
23452347

2346-
if (source instanceof Collection) {
2347-
throw new MappingException(
2348-
String.format(INCOMPATIBLE_TYPES, source, BasicDBList.class, typeHint.getType(), getPath()));
2349-
}
2350-
23512348
if (BsonUtils.supportsBson(source)) {
23522349
return (S) documentConverter.convert(context, BsonUtils.asBson(source), typeHint);
23532350
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,18 @@ public org.bson.Document write(@Nullable String domainValue, MongoConversionCont
28312831
assertThat(converter.read(Cyclic.class, source).cycle.value).isEqualTo("v2");
28322832
}
28332833

2834+
@Test // GH-4371
2835+
void shouldConvertTypesToStringTargetType() {
2836+
2837+
org.bson.Document source = org.bson.Document.parse("""
2838+
{
2839+
city : ["Gotham", "Metropolis"]
2840+
}
2841+
""");
2842+
2843+
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
2844+
}
2845+
28342846
static class GenericType<T> {
28352847
T content;
28362848
}

0 commit comments

Comments
 (0)