|
22 | 22 |
|
23 | 23 | import lombok.EqualsAndHashCode;
|
24 | 24 | import lombok.RequiredArgsConstructor;
|
| 25 | +import lombok.ToString; |
25 | 26 |
|
26 | 27 | import java.math.BigDecimal;
|
27 | 28 | import java.math.BigInteger;
|
|
70 | 71 | import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.NestedType;
|
71 | 72 | import org.springframework.data.mongodb.core.convert.DocumentAccessorUnitTests.ProjectingType;
|
72 | 73 | import org.springframework.data.mongodb.core.convert.MappingMongoConverterUnitTests.ClassWithMapUsingEnumAsKey.FooBarEnum;
|
| 74 | +import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter; |
73 | 75 | import org.springframework.data.mongodb.core.geo.Sphere;
|
74 | 76 | import org.springframework.data.mongodb.core.mapping.Document;
|
75 | 77 | import org.springframework.data.mongodb.core.mapping.Embedded;
|
|
81 | 83 | import org.springframework.data.mongodb.core.mapping.TextScore;
|
82 | 84 | import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
|
83 | 85 | import org.springframework.data.util.ClassTypeInformation;
|
| 86 | +import org.springframework.lang.Nullable; |
84 | 87 | import org.springframework.test.util.ReflectionTestUtils;
|
85 | 88 |
|
86 | 89 | import com.mongodb.BasicDBList;
|
@@ -2898,4 +2901,62 @@ public Person onAfterConvert(Person entity, org.bson.Document document, String c
|
2898 | 2901 | }
|
2899 | 2902 | }
|
2900 | 2903 |
|
| 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 | + } |
2901 | 2962 | }
|
0 commit comments