Skip to content

Commit b9af920

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 fb43e85 commit b9af920

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -2329,8 +2329,10 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23292329
if (source instanceof Collection) {
23302330

23312331
Class<?> rawType = typeHint.getType();
2332-
if (!Object.class.equals(rawType)) {
2332+
if (!Object.class.equals(rawType) && !String.class.equals(rawType)) {
2333+
23332334
if (!rawType.isArray() && !ClassUtils.isAssignable(Iterable.class, rawType)) {
2335+
23342336
throw new MappingException(
23352337
String.format(INCOMPATIBLE_TYPES, source, source.getClass(), rawType, getPath()));
23362338
}
@@ -2359,11 +2361,6 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
23592361
return (S) dbRefConverter.convert(context, (DBRef) source, typeHint);
23602362
}
23612363

2362-
if (source instanceof Collection) {
2363-
throw new MappingException(
2364-
String.format(INCOMPATIBLE_TYPES, source, BasicDBList.class, typeHint.getType(), getPath()));
2365-
}
2366-
23672364
if (BsonUtils.supportsBson(source)) {
23682365
return (S) documentConverter.convert(context, BsonUtils.asBson(source), typeHint);
23692366
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -2858,6 +2858,14 @@ public org.bson.Document write(@Nullable String domainValue, MongoConversionCont
28582858
assertThat(converter.read(Cyclic.class, source).cycle.value).isEqualTo("v2");
28592859
}
28602860

2861+
@Test // GH-4371
2862+
void shouldConvertTypesToStringTargetType() {
2863+
2864+
org.bson.Document source = org.bson.Document.parse("{\n" + " city : [\"Gotham\", \"Metropolis\"]\n" + "}\n");
2865+
2866+
assertThat(converter.read(Address.class, source).city).isEqualTo("Gotham,Metropolis");
2867+
}
2868+
28612869
static class GenericType<T> {
28622870
T content;
28632871
}

0 commit comments

Comments
 (0)