Skip to content

Commit a35c4f2

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 8803e03 commit a35c4f2

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
@@ -2315,8 +2315,10 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23152315
if (source instanceof Collection) {
23162316

23172317
Class<?> rawType = typeHint.getType();
2318-
if (!Object.class.equals(rawType)) {
2318+
if (!Object.class.equals(rawType) && !String.class.equals(rawType)) {
2319+
23192320
if (!rawType.isArray() && !ClassUtils.isAssignable(Iterable.class, rawType)) {
2321+
23202322
throw new MappingException(
23212323
String.format(INCOMPATIBLE_TYPES, source, source.getClass(), rawType, getPath()));
23222324
}
@@ -2345,11 +2347,6 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23452347
return (S) dbRefConverter.convert(context, (DBRef) source, typeHint);
23462348
}
23472349

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

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)