Skip to content

Commit 78225dd

Browse files
Add testcase
1 parent f6f2060 commit 78225dd

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import lombok.EqualsAndHashCode;
2424
import lombok.RequiredArgsConstructor;
25+
import lombok.ToString;
2526

2627
import java.math.BigDecimal;
2728
import java.math.BigInteger;
@@ -70,6 +71,7 @@
7071
import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType;
7172
import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType;
7273
import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum;
74+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
7375
import org.springframework.data.mongodb.core.geo.Sphere;
7476
import org.springframework.data.mongodb.core.mapping.Document;
7577
import org.springframework.data.mongodb.core.mapping.Embedded;
@@ -81,6 +83,7 @@
8183
import org.springframework.data.mongodb.core.mapping.TextScore;
8284
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
8385
import org.springframework.data.util.ClassTypeInformation;
86+
import org.springframework.lang.Nullable;
8487
import org.springframework.test.util.ReflectionTestUtils;
8588

8689
import com.mongodb.BasicDBList;
@@ -2898,4 +2901,62 @@ public Person onAfterConvert(Person entity, org.bson.Document document, String c
28982901
}
28992902
}
29002903

2904+
@ToString
2905+
static class TypeUsingAnotherTypeWithGenericsInConstructor {
2906+
2907+
private final Wrapper<MyEnum> myEnum;
2908+
2909+
@PersistenceConstructor
2910+
public TypeUsingAnotherTypeWithGenericsInConstructor(Wrapper<MyEnum> myEnum) {
2911+
this.myEnum = myEnum;
2912+
}
2913+
2914+
public Wrapper<MyEnum> getMyEnum() {
2915+
return myEnum;
2916+
}
2917+
}
2918+
2919+
public enum MyEnum {
2920+
ONE, NOT_ONE
2921+
}
2922+
2923+
@ToString
2924+
static class Wrapper<T> {
2925+
2926+
final T value;
2927+
2928+
public Wrapper(T value) {
2929+
this.value = value;
2930+
}
2931+
2932+
public T getValue() {
2933+
return value;
2934+
}
2935+
}
2936+
2937+
@ReadingConverter
2938+
static class MyEnumConverter implements Converter<String, MyEnum> {
2939+
2940+
@Nullable
2941+
@Override
2942+
public MyEnum convert(String source) {
2943+
return source.equals("1") ? MyEnum.ONE : MyEnum.NOT_ONE;
2944+
}
2945+
}
2946+
2947+
@Test // GH-3567
2948+
void shouldUseConverterOnConstructor() {
2949+
2950+
converter = new MappingMongoConverter(resolver, mappingContext);
2951+
2952+
converter.setCustomConversions(new MongoCustomConversions(MongoConverterConfigurationAdapter
2953+
.from(Collections.singletonList(new MyEnumConverter()))));
2954+
converter.afterPropertiesSet();
2955+
2956+
org.bson.Document source = new org.bson.Document("myEnum", new org.bson.Document("value", "1"));
2957+
2958+
TypeUsingAnotherTypeWithGenericsInConstructor target = converter.read(TypeUsingAnotherTypeWithGenericsInConstructor.class, source);
2959+
assertThat(target.getMyEnum()).isNotNull();
2960+
assertThat(target.getMyEnum().getValue()).isEqualTo(MyEnum.ONE);
2961+
}
29012962
}

0 commit comments

Comments
 (0)