38
38
import org .bson .conversions .Bson ;
39
39
import org .bson .json .JsonReader ;
40
40
import org .bson .types .ObjectId ;
41
-
42
41
import org .springframework .beans .BeansException ;
43
42
import org .springframework .beans .factory .BeanClassLoaderAware ;
44
43
import org .springframework .context .ApplicationContext ;
@@ -186,7 +185,7 @@ protected ConversionContext getConversionContext(ObjectPath path) {
186
185
187
186
Assert .notNull (path , "ObjectPath must not be null" );
188
187
189
- return new ConversionContext (conversions , path , this ::readDocument , this ::readCollectionOrArray , this ::readMap ,
188
+ return new ConversionContext (this , conversions , path , this ::readDocument , this ::readCollectionOrArray , this ::readMap ,
190
189
this ::readDBRef , this ::getPotentiallyConvertedSimpleRead );
191
190
}
192
191
@@ -316,7 +315,7 @@ public <R> R project(EntityProjection<R, ?> projection, Bson bson) {
316
315
return (R ) read (typeToRead , bson );
317
316
}
318
317
319
- ProjectingConversionContext context = new ProjectingConversionContext (conversions , ObjectPath .ROOT ,
318
+ ProjectingConversionContext context = new ProjectingConversionContext (this , conversions , ObjectPath .ROOT ,
320
319
this ::readCollectionOrArray , this ::readMap , this ::readDBRef , this ::getPotentiallyConvertedSimpleRead ,
321
320
projection );
322
321
@@ -399,11 +398,11 @@ class ProjectingConversionContext extends ConversionContext {
399
398
400
399
private final EntityProjection <?, ?> returnedTypeDescriptor ;
401
400
402
- ProjectingConversionContext (CustomConversions customConversions , ObjectPath path ,
401
+ ProjectingConversionContext (MongoConverter sourceConverter , CustomConversions customConversions , ObjectPath path ,
403
402
ContainerValueConverter <Collection <?>> collectionConverter , ContainerValueConverter <Bson > mapConverter ,
404
403
ContainerValueConverter <DBRef > dbRefConverter , ValueConverter <Object > elementConverter ,
405
404
EntityProjection <?, ?> projection ) {
406
- super (customConversions , path ,
405
+ super (sourceConverter , customConversions , path ,
407
406
(context , source , typeHint ) -> doReadOrProject (context , source , typeHint , projection ),
408
407
409
408
collectionConverter , mapConverter , dbRefConverter , elementConverter );
@@ -419,13 +418,13 @@ public ConversionContext forProperty(String name) {
419
418
mapConverter , dbRefConverter , elementConverter );
420
419
}
421
420
422
- return new ProjectingConversionContext (conversions , path , collectionConverter , mapConverter , dbRefConverter ,
421
+ return new ProjectingConversionContext (sourceConverter , conversions , path , collectionConverter , mapConverter , dbRefConverter ,
423
422
elementConverter , property );
424
423
}
425
424
426
425
@ Override
427
426
public ConversionContext withPath (ObjectPath currentPath ) {
428
- return new ProjectingConversionContext (conversions , currentPath , collectionConverter , mapConverter ,
427
+ return new ProjectingConversionContext (sourceConverter , conversions , currentPath , collectionConverter , mapConverter ,
429
428
dbRefConverter , elementConverter , returnedTypeDescriptor );
430
429
}
431
430
}
@@ -965,6 +964,11 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
965
964
TypeInformation <?> valueType = ClassTypeInformation .from (obj .getClass ());
966
965
TypeInformation <?> type = prop .getTypeInformation ();
967
966
967
+ if (conversions .hasPropertyValueConverter (prop )) {
968
+ accessor .put (prop , conversions .getPropertyValueConverter (prop ).write (obj , new MongoConversionContext (prop , this )));
969
+ return ;
970
+ }
971
+
968
972
if (prop .isUnwrapped ()) {
969
973
970
974
Document target = new Document ();
@@ -1294,6 +1298,12 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
1294
1298
1295
1299
private void writeSimpleInternal (@ Nullable Object value , Bson bson , MongoPersistentProperty property ) {
1296
1300
DocumentAccessor accessor = new DocumentAccessor (bson );
1301
+
1302
+ if (conversions .hasPropertyValueConverter (property )) {
1303
+ accessor .put (property , conversions .getPropertyValueConverter (property ).write (value , new MongoConversionContext (property , this )));
1304
+ return ;
1305
+ }
1306
+
1297
1307
accessor .put (property , getPotentiallyConvertedSimpleWrite (value ,
1298
1308
property .hasExplicitWriteTarget () ? property .getFieldType () : Object .class ));
1299
1309
}
@@ -1957,6 +1967,11 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1957
1967
return null ;
1958
1968
}
1959
1969
1970
+ if (context .conversions .hasPropertyValueConverter (property )) {
1971
+
1972
+ return (T ) context .conversions .getPropertyValueConverter (property ).read (value , new MongoConversionContext (property , context .sourceConverter ));
1973
+ }
1974
+
1960
1975
return (T ) context .convert (value , property .getTypeInformation ());
1961
1976
}
1962
1977
@@ -2182,6 +2197,7 @@ public org.springframework.data.util.TypeInformation<? extends S> specialize(Cla
2182
2197
*/
2183
2198
protected static class ConversionContext {
2184
2199
2200
+ final MongoConverter sourceConverter ;
2185
2201
final org .springframework .data .convert .CustomConversions conversions ;
2186
2202
final ObjectPath path ;
2187
2203
final ContainerValueConverter <Bson > documentConverter ;
@@ -2190,11 +2206,12 @@ protected static class ConversionContext {
2190
2206
final ContainerValueConverter <DBRef > dbRefConverter ;
2191
2207
final ValueConverter <Object > elementConverter ;
2192
2208
2193
- ConversionContext (org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
2209
+ ConversionContext (MongoConverter sourceConverter , org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
2194
2210
ContainerValueConverter <Bson > documentConverter , ContainerValueConverter <Collection <?>> collectionConverter ,
2195
2211
ContainerValueConverter <Bson > mapConverter , ContainerValueConverter <DBRef > dbRefConverter ,
2196
2212
ValueConverter <Object > elementConverter ) {
2197
2213
2214
+ this .sourceConverter = sourceConverter ;
2198
2215
this .conversions = customConversions ;
2199
2216
this .path = path ;
2200
2217
this .documentConverter = documentConverter ;
@@ -2276,7 +2293,7 @@ public ConversionContext withPath(ObjectPath currentPath) {
2276
2293
2277
2294
Assert .notNull (currentPath , "ObjectPath must not be null" );
2278
2295
2279
- return new ConversionContext (conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
2296
+ return new ConversionContext (sourceConverter , conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
2280
2297
dbRefConverter , elementConverter );
2281
2298
}
2282
2299
0 commit comments