3838import org .bson .conversions .Bson ;
3939import org .bson .json .JsonReader ;
4040import org .bson .types .ObjectId ;
41-
4241import org .springframework .beans .BeansException ;
4342import org .springframework .beans .factory .BeanClassLoaderAware ;
4443import org .springframework .context .ApplicationContext ;
@@ -186,7 +185,7 @@ protected ConversionContext getConversionContext(ObjectPath path) {
186185
187186 Assert .notNull (path , "ObjectPath must not be null" );
188187
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 ,
190189 this ::readDBRef , this ::getPotentiallyConvertedSimpleRead );
191190 }
192191
@@ -316,7 +315,7 @@ public <R> R project(EntityProjection<R, ?> projection, Bson bson) {
316315 return (R ) read (typeToRead , bson );
317316 }
318317
319- ProjectingConversionContext context = new ProjectingConversionContext (conversions , ObjectPath .ROOT ,
318+ ProjectingConversionContext context = new ProjectingConversionContext (this , conversions , ObjectPath .ROOT ,
320319 this ::readCollectionOrArray , this ::readMap , this ::readDBRef , this ::getPotentiallyConvertedSimpleRead ,
321320 projection );
322321
@@ -399,11 +398,11 @@ class ProjectingConversionContext extends ConversionContext {
399398
400399 private final EntityProjection <?, ?> returnedTypeDescriptor ;
401400
402- ProjectingConversionContext (CustomConversions customConversions , ObjectPath path ,
401+ ProjectingConversionContext (MongoConverter sourceConverter , CustomConversions customConversions , ObjectPath path ,
403402 ContainerValueConverter <Collection <?>> collectionConverter , ContainerValueConverter <Bson > mapConverter ,
404403 ContainerValueConverter <DBRef > dbRefConverter , ValueConverter <Object > elementConverter ,
405404 EntityProjection <?, ?> projection ) {
406- super (customConversions , path ,
405+ super (sourceConverter , customConversions , path ,
407406 (context , source , typeHint ) -> doReadOrProject (context , source , typeHint , projection ),
408407
409408 collectionConverter , mapConverter , dbRefConverter , elementConverter );
@@ -419,13 +418,13 @@ public ConversionContext forProperty(String name) {
419418 mapConverter , dbRefConverter , elementConverter );
420419 }
421420
422- return new ProjectingConversionContext (conversions , path , collectionConverter , mapConverter , dbRefConverter ,
421+ return new ProjectingConversionContext (sourceConverter , conversions , path , collectionConverter , mapConverter , dbRefConverter ,
423422 elementConverter , property );
424423 }
425424
426425 @ Override
427426 public ConversionContext withPath (ObjectPath currentPath ) {
428- return new ProjectingConversionContext (conversions , currentPath , collectionConverter , mapConverter ,
427+ return new ProjectingConversionContext (sourceConverter , conversions , currentPath , collectionConverter , mapConverter ,
429428 dbRefConverter , elementConverter , returnedTypeDescriptor );
430429 }
431430 }
@@ -965,6 +964,11 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
965964 TypeInformation <?> valueType = ClassTypeInformation .from (obj .getClass ());
966965 TypeInformation <?> type = prop .getTypeInformation ();
967966
967+ if (conversions .hasPropertyValueConverter (prop )) {
968+ accessor .put (prop , conversions .getPropertyValueConverter (prop ).write (obj , new MongoConversionContext (prop , this )));
969+ return ;
970+ }
971+
968972 if (prop .isUnwrapped ()) {
969973
970974 Document target = new Document ();
@@ -1294,6 +1298,12 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
12941298
12951299 private void writeSimpleInternal (@ Nullable Object value , Bson bson , MongoPersistentProperty property ) {
12961300 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+
12971307 accessor .put (property , getPotentiallyConvertedSimpleWrite (value ,
12981308 property .hasExplicitWriteTarget () ? property .getFieldType () : Object .class ));
12991309 }
@@ -1957,6 +1967,11 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19571967 return null ;
19581968 }
19591969
1970+ if (context .conversions .hasPropertyValueConverter (property )) {
1971+
1972+ return (T ) context .conversions .getPropertyValueConverter (property ).read (value , new MongoConversionContext (property , context .sourceConverter ));
1973+ }
1974+
19601975 return (T ) context .convert (value , property .getTypeInformation ());
19611976 }
19621977
@@ -2182,6 +2197,7 @@ public org.springframework.data.util.TypeInformation<? extends S> specialize(Cla
21822197 */
21832198 protected static class ConversionContext {
21842199
2200+ final MongoConverter sourceConverter ;
21852201 final org .springframework .data .convert .CustomConversions conversions ;
21862202 final ObjectPath path ;
21872203 final ContainerValueConverter <Bson > documentConverter ;
@@ -2190,11 +2206,12 @@ protected static class ConversionContext {
21902206 final ContainerValueConverter <DBRef > dbRefConverter ;
21912207 final ValueConverter <Object > elementConverter ;
21922208
2193- ConversionContext (org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
2209+ ConversionContext (MongoConverter sourceConverter , org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
21942210 ContainerValueConverter <Bson > documentConverter , ContainerValueConverter <Collection <?>> collectionConverter ,
21952211 ContainerValueConverter <Bson > mapConverter , ContainerValueConverter <DBRef > dbRefConverter ,
21962212 ValueConverter <Object > elementConverter ) {
21972213
2214+ this .sourceConverter = sourceConverter ;
21982215 this .conversions = customConversions ;
21992216 this .path = path ;
22002217 this .documentConverter = documentConverter ;
@@ -2276,7 +2293,7 @@ public ConversionContext withPath(ObjectPath currentPath) {
22762293
22772294 Assert .notNull (currentPath , "ObjectPath must not be null" );
22782295
2279- return new ConversionContext (conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
2296+ return new ConversionContext (sourceConverter , conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
22802297 dbRefConverter , elementConverter );
22812298 }
22822299
0 commit comments