Skip to content

Commit 23177fe

Browse files
christophstroblmp911de
authored andcommitted
Custom Converter should also be applicable for simple types.
This commit fixes a regression that prevented custom converters from being applied to types considered store native ones. Original pull request: #3703. Fixes #3670
1 parent f3b90c2 commit 23177fe

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1173,14 +1173,18 @@ protected Object getPotentiallyConvertedSimpleRead(Object value, TypeInformation
11731173
@SuppressWarnings({ "rawtypes", "unchecked" })
11741174
private Object getPotentiallyConvertedSimpleRead(Object value, @Nullable Class<?> target) {
11751175

1176-
if (target == null || ClassUtils.isAssignableValue(target, value)) {
1176+
if (target == null) {
11771177
return value;
11781178
}
11791179

11801180
if (conversions.hasCustomReadTarget(value.getClass(), target)) {
11811181
return doConvert(value, target);
11821182
}
11831183

1184+
if (ClassUtils.isAssignableValue(target, value)) {
1185+
return value;
1186+
}
1187+
11841188
if (Enum.class.isAssignableFrom(target)) {
11851189
return Enum.valueOf((Class<Enum>) target, value.toString());
11861190
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.time.temporal.ChronoUnit;
3131
import java.util.*;
3232

33+
import org.bson.types.Binary;
3334
import org.bson.types.Code;
3435
import org.bson.types.Decimal128;
3536
import org.bson.types.ObjectId;
@@ -2568,6 +2569,21 @@ void readsMapContainingNullValue() {
25682569
.containsEntry("item3", "i3");
25692570
}
25702571

2572+
@Test // GH-3670
2573+
void appliesCustomConverterEvenToSimpleTypes() {
2574+
2575+
converter = new MappingMongoConverter(resolver, mappingContext);
2576+
converter.setCustomConversions(MongoCustomConversions.create(it -> {
2577+
it.registerConverter(new MongoSimpleTypeConverter());
2578+
}));
2579+
converter.afterPropertiesSet();
2580+
2581+
org.bson.Document source = new org.bson.Document("content", new Binary(new byte[] {0x00, 0x42}));
2582+
2583+
GenericType<Object> target = converter.read(GenericType.class, source);
2584+
assertThat(target.content).isInstanceOf(byte[].class);
2585+
}
2586+
25712587
static class GenericType<T> {
25722588
T content;
25732589
}
@@ -3136,6 +3152,15 @@ public TypeImplementingMap convert(org.bson.Document source) {
31363152
}
31373153
}
31383154

3155+
@ReadingConverter
3156+
public static class MongoSimpleTypeConverter implements Converter<Binary, Object> {
3157+
3158+
@Override
3159+
public byte[] convert(Binary source) {
3160+
return source.getData();
3161+
}
3162+
}
3163+
31393164
static class TypeWrappingTypeImplementingMap {
31403165

31413166
String id;

0 commit comments

Comments
 (0)