|
37 | 37 | import org.junit.jupiter.api.Disabled;
|
38 | 38 | import org.junit.jupiter.api.Test;
|
39 | 39 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 40 | +import org.junit.jupiter.params.ParameterizedTest; |
| 41 | +import org.junit.jupiter.params.provider.ValueSource; |
40 | 42 | import org.mockito.Mock;
|
41 | 43 | import org.mockito.Mockito;
|
42 | 44 | import org.mockito.junit.jupiter.MockitoExtension;
|
|
80 | 82 | import org.springframework.data.mongodb.core.mapping.FieldName.Type;
|
81 | 83 | import org.springframework.data.mongodb.core.mapping.FieldType;
|
82 | 84 | import org.springframework.data.mongodb.core.mapping.MongoField;
|
| 85 | +import org.springframework.data.mongodb.core.mapping.MongoId; |
83 | 86 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
84 | 87 | import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
|
85 | 88 | import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
|
@@ -2955,6 +2958,27 @@ void readShouldAllowDotsInMapKeyNameIfConfigured() {
|
2955 | 2958 | assertThat(target.mapOfPersons).containsEntry("map.key.with.dots", person);
|
2956 | 2959 | }
|
2957 | 2960 |
|
| 2961 | + @ValueSource(classes = { ComplexIdAndNoAnnotation.class, ComplexIdAndIdAnnotation.class, ComplexIdAndMongoIdAnnotation.class, ComplexIdAndFieldAnnotation.class }) |
| 2962 | + @ParameterizedTest // GH-4524 |
| 2963 | + void projectShouldReadComplexIdType(Class<?> projectionTargetType) { |
| 2964 | + |
| 2965 | + EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(), |
| 2966 | + EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy() |
| 2967 | + .and((target, underlyingType) -> !converter.conversions.isSimpleType(target)), |
| 2968 | + mappingContext); |
| 2969 | + |
| 2970 | + ComplexId idValue = ComplexId.of(101L); |
| 2971 | + org.bson.Document source = new org.bson.Document("_id", new org.bson.Document("innerId", idValue.innerId)) |
| 2972 | + .append("value", "abc").append("_class", ComplexIdAndNoAnnotation.class.getName()); |
| 2973 | + |
| 2974 | + EntityProjection<?, ComplexIdAndNoAnnotation> projection = introspector.introspect(projectionTargetType, |
| 2975 | + ComplexIdAndNoAnnotation.class); |
| 2976 | + |
| 2977 | + assertThat(converter.project(projection, source)) // |
| 2978 | + .isInstanceOf(projectionTargetType) // |
| 2979 | + .extracting("id").isEqualTo(idValue); |
| 2980 | + } |
| 2981 | + |
2958 | 2982 | org.bson.Document write(Object source) {
|
2959 | 2983 |
|
2960 | 2984 | org.bson.Document target = new org.bson.Document();
|
@@ -3219,7 +3243,33 @@ static class ClassWithComplexId {
|
3219 | 3243 | }
|
3220 | 3244 |
|
3221 | 3245 | static class ComplexId {
|
| 3246 | + |
3222 | 3247 | Long innerId;
|
| 3248 | + |
| 3249 | + static ComplexId of(Long value) { |
| 3250 | + |
| 3251 | + ComplexId id = new ComplexId(); |
| 3252 | + id.innerId = value; |
| 3253 | + return id; |
| 3254 | + } |
| 3255 | + |
| 3256 | + @Override |
| 3257 | + public boolean equals(Object o) { |
| 3258 | + |
| 3259 | + if (o == this) { |
| 3260 | + return true; |
| 3261 | + } |
| 3262 | + if (o == null || getClass() != o.getClass()) { |
| 3263 | + return false; |
| 3264 | + } |
| 3265 | + ComplexId complexId = (ComplexId) o; |
| 3266 | + return Objects.equals(innerId, complexId.innerId); |
| 3267 | + } |
| 3268 | + |
| 3269 | + @Override |
| 3270 | + public int hashCode() { |
| 3271 | + return Objects.hash(innerId); |
| 3272 | + } |
3223 | 3273 | }
|
3224 | 3274 |
|
3225 | 3275 | static class TypWithCollectionConstructor {
|
@@ -4031,7 +4081,32 @@ static class WithPropertyHavingDotsInFieldName {
|
4031 | 4081 |
|
4032 | 4082 | @Field(name = "field.name.with.dots", nameType = Type.KEY)
|
4033 | 4083 | String value;
|
| 4084 | + } |
| 4085 | + |
| 4086 | + static class ComplexIdAndFieldAnnotation { |
| 4087 | + |
| 4088 | + @Field("_id") // |
| 4089 | + ComplexId id; |
| 4090 | + String value; |
| 4091 | + } |
| 4092 | + |
| 4093 | + static class ComplexIdAndMongoIdAnnotation { |
4034 | 4094 |
|
| 4095 | + @MongoId // |
| 4096 | + ComplexId id; |
| 4097 | + String value; |
4035 | 4098 | }
|
4036 | 4099 |
|
| 4100 | + static class ComplexIdAndIdAnnotation { |
| 4101 | + |
| 4102 | + @Id // |
| 4103 | + ComplexId id; |
| 4104 | + String value; |
| 4105 | + } |
| 4106 | + |
| 4107 | + static class ComplexIdAndNoAnnotation { |
| 4108 | + |
| 4109 | + ComplexId id; |
| 4110 | + String value; |
| 4111 | + } |
4037 | 4112 | }
|
0 commit comments