32
32
import java .util .Map ;
33
33
34
34
import org .bson .Document ;
35
+ import org .bson .types .ObjectId ;
35
36
import org .junit .jupiter .api .BeforeEach ;
36
37
import org .junit .jupiter .api .Disabled ;
37
38
import org .junit .jupiter .api .Test ;
44
45
import org .springframework .data .mongodb .core .mapping .DocumentPointer ;
45
46
import org .springframework .data .mongodb .core .mapping .DocumentReference ;
46
47
import org .springframework .data .mongodb .core .mapping .Field ;
48
+ import org .springframework .data .mongodb .core .mapping .FieldType ;
49
+ import org .springframework .data .mongodb .core .mapping .MongoId ;
47
50
import org .springframework .data .mongodb .core .query .Update ;
48
51
import org .springframework .data .mongodb .test .util .Client ;
49
52
import org .springframework .data .mongodb .test .util .MongoClientExtension ;
@@ -106,6 +109,26 @@ void writeSimpleTypeReference() {
106
109
assertThat (target .get ("simpleValueRef" )).isEqualTo ("ref-1" );
107
110
}
108
111
112
+ @ Test // GH-3782
113
+ void writeTypeReferenceHavingCustomizedIdTargetType () {
114
+
115
+ ObjectId expectedIdValue = new ObjectId ();
116
+ String rootCollectionName = template .getCollectionName (SingleRefRoot .class );
117
+
118
+ SingleRefRoot source = new SingleRefRoot ();
119
+ source .id = "root-1" ;
120
+ source .customIdTargetRef = new ObjectRefHavingCustomizedIdTargetType (expectedIdValue .toString (),
121
+ "me-the-referenced-object" );
122
+
123
+ template .save (source );
124
+
125
+ Document target = template .execute (db -> {
126
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
127
+ });
128
+
129
+ assertThat (target .get ("customIdTargetRef" )).isEqualTo (expectedIdValue );
130
+ }
131
+
109
132
@ Test // GH-3602
110
133
void writeMapTypeReference () {
111
134
@@ -126,6 +149,26 @@ void writeMapTypeReference() {
126
149
assertThat (target .get ("mapValueRef" , Map .class )).containsEntry ("frodo" , "ref-1" ).containsEntry ("bilbo" , "ref-2" );
127
150
}
128
151
152
+ @ Test // GH-3782
153
+ void writeMapOfTypeReferenceHavingCustomizedIdTargetType () {
154
+
155
+ ObjectId expectedIdValue = new ObjectId ();
156
+ String rootCollectionName = template .getCollectionName (CollectionRefRoot .class );
157
+
158
+ CollectionRefRoot source = new CollectionRefRoot ();
159
+ source .id = "root-1" ;
160
+ source .customIdTargetRefMap = Collections .singletonMap ("frodo" ,
161
+ new ObjectRefHavingCustomizedIdTargetType (expectedIdValue .toString (), "me-the-referenced-object" ));
162
+
163
+ template .save (source );
164
+
165
+ Document target = template .execute (db -> {
166
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
167
+ });
168
+
169
+ assertThat (target .get ("customIdTargetRefMap" , Map .class )).containsEntry ("frodo" , expectedIdValue );
170
+ }
171
+
129
172
@ Test // GH-3602
130
173
void writeCollectionOfSimpleTypeReference () {
131
174
@@ -145,6 +188,26 @@ void writeCollectionOfSimpleTypeReference() {
145
188
assertThat (target .get ("simpleValueRef" , List .class )).containsExactly ("ref-1" , "ref-2" );
146
189
}
147
190
191
+ @ Test // GH-3782
192
+ void writeListOfTypeReferenceHavingCustomizedIdTargetType () {
193
+
194
+ ObjectId expectedIdValue = new ObjectId ();
195
+ String rootCollectionName = template .getCollectionName (CollectionRefRoot .class );
196
+
197
+ CollectionRefRoot source = new CollectionRefRoot ();
198
+ source .id = "root-1" ;
199
+ source .customIdTargetRefList = Collections .singletonList (
200
+ new ObjectRefHavingCustomizedIdTargetType (expectedIdValue .toString (), "me-the-referenced-object" ));
201
+
202
+ template .save (source );
203
+
204
+ Document target = template .execute (db -> {
205
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
206
+ });
207
+
208
+ assertThat (target .get ("customIdTargetRefList" , List .class )).containsExactly (expectedIdValue );
209
+ }
210
+
148
211
@ Test // GH-3602
149
212
void writeObjectTypeReference () {
150
213
@@ -739,6 +802,26 @@ void updateReferenceWithValue() {
739
802
assertThat (target ).containsEntry ("toB" , "b" );
740
803
}
741
804
805
+ @ Test // GH-3782
806
+ void updateReferenceHavingCustomizedIdTargetType () {
807
+
808
+ ObjectId expectedIdValue = new ObjectId ();
809
+ String rootCollectionName = template .getCollectionName (SingleRefRoot .class );
810
+
811
+ SingleRefRoot root = new SingleRefRoot ();
812
+ root .id = "root-1" ;
813
+ template .save (root );
814
+
815
+ template .update (SingleRefRoot .class ).apply (new Update ().set ("customIdTargetRef" ,
816
+ new ObjectRefHavingCustomizedIdTargetType (expectedIdValue .toString (), "b" ))).first ();
817
+
818
+ Document target = template .execute (db -> {
819
+ return db .getCollection (rootCollectionName ).find (Filters .eq ("_id" , "root-1" )).first ();
820
+ });
821
+
822
+ assertThat (target ).containsEntry ("customIdTargetRef" , expectedIdValue );
823
+ }
824
+
742
825
@ Test // GH-3602
743
826
void updateReferenceCollectionWithEntity () {
744
827
@@ -998,6 +1081,8 @@ static class SingleRefRoot {
998
1081
999
1082
@ DocumentReference (lookup = "{ 'refKey1' : '?#{refKey1}', 'refKey2' : '?#{refKey2}' }" , lazy = true ) //
1000
1083
ObjectRefOnNonIdField lazyObjectValueRefOnNonIdFields ;
1084
+
1085
+ @ DocumentReference ObjectRefHavingCustomizedIdTargetType customIdTargetRef ;
1001
1086
}
1002
1087
1003
1088
@ Data
@@ -1027,6 +1112,10 @@ static class CollectionRefRoot {
1027
1112
1028
1113
@ DocumentReference (lookup = "{ 'refKey1' : '?#{refKey1}', 'refKey2' : '?#{refKey2}' }" ) //
1029
1114
List <ObjectRefOnNonIdField > objectValueRefOnNonIdFields ;
1115
+
1116
+ @ DocumentReference List <ObjectRefHavingCustomizedIdTargetType > customIdTargetRefList ;
1117
+
1118
+ @ DocumentReference Map <String , ObjectRefHavingCustomizedIdTargetType > customIdTargetRefMap ;
1030
1119
}
1031
1120
1032
1121
@ FunctionalInterface
@@ -1094,6 +1183,14 @@ public Object toReference() {
1094
1183
}
1095
1184
}
1096
1185
1186
+ @ Data
1187
+ @ AllArgsConstructor
1188
+ static class ObjectRefHavingCustomizedIdTargetType {
1189
+
1190
+ @ MongoId (targetType = FieldType .OBJECT_ID ) String id ;
1191
+ String name ;
1192
+ }
1193
+
1097
1194
static class ReferencableConverter implements Converter <ReferenceAble , DocumentPointer <Object >> {
1098
1195
1099
1196
@ Nullable
@@ -1196,5 +1293,4 @@ static class UsingAtReference {
1196
1293
@ Reference //
1197
1294
Publisher publisher ;
1198
1295
}
1199
-
1200
1296
}
0 commit comments