Skip to content

Commit f987217

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 92a2297 commit f987217

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

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

1043-
if (target == null || ClassUtils.isAssignableValue(target, value)) {
1043+
if (target == null) {
10441044
return value;
10451045
}
10461046

10471047
if (conversions.hasCustomReadTarget(value.getClass(), target)) {
10481048
return doConvert(value, target);
10491049
}
10501050

1051+
if (ClassUtils.isAssignableValue(target, value)) {
1052+
return value;
1053+
}
1054+
10511055
if (Enum.class.isAssignableFrom(target)) {
10521056
return Enum.valueOf((Class<Enum>) target, value.toString());
10531057
}

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;
@@ -2555,6 +2556,21 @@ void readsMapContainingNullValue() {
25552556
.containsEntry("item3", "i3");
25562557
}
25572558

2559+
@Test // GH-3670
2560+
void appliesCustomConverterEvenToSimpleTypes() {
2561+
2562+
converter = new MappingMongoConverter(resolver, mappingContext);
2563+
converter.setCustomConversions(MongoCustomConversions.create(it -> {
2564+
it.registerConverter(new MongoSimpleTypeConverter());
2565+
}));
2566+
converter.afterPropertiesSet();
2567+
2568+
org.bson.Document source = new org.bson.Document("content", new Binary(new byte[] {0x00, 0x42}));
2569+
2570+
GenericType<Object> target = converter.read(GenericType.class, source);
2571+
assertThat(target.content).isInstanceOf(byte[].class);
2572+
}
2573+
25582574
static class GenericType<T> {
25592575
T content;
25602576
}
@@ -3123,6 +3139,15 @@ public TypeImplementingMap convert(org.bson.Document source) {
31233139
}
31243140
}
31253141

3142+
@ReadingConverter
3143+
public static class MongoSimpleTypeConverter implements Converter<Binary, Object> {
3144+
3145+
@Override
3146+
public byte[] convert(Binary source) {
3147+
return source.getData();
3148+
}
3149+
}
3150+
31263151
static class TypeWrappingTypeImplementingMap {
31273152

31283153
String id;

0 commit comments

Comments
 (0)