@@ -176,7 +176,7 @@ protected ConversionContext getConversionContext(ObjectPath path) {
176176
177177 Assert .notNull (path , "ObjectPath must not be null" );
178178
179- return new ConversionContext (conversions , path , this ::readDocument , this ::readCollectionOrArray , this ::readMap ,
179+ return new ConversionContext (this , conversions , path , this ::readDocument , this ::readCollectionOrArray , this ::readMap ,
180180 this ::readDBRef , this ::getPotentiallyConvertedSimpleRead );
181181 }
182182
@@ -294,7 +294,7 @@ public <R> R project(EntityProjection<R, ?> projection, Bson bson) {
294294 return (R ) read (typeToRead , bson );
295295 }
296296
297- ProjectingConversionContext context = new ProjectingConversionContext (conversions , ObjectPath .ROOT ,
297+ ProjectingConversionContext context = new ProjectingConversionContext (this , conversions , ObjectPath .ROOT ,
298298 this ::readCollectionOrArray , this ::readMap , this ::readDBRef , this ::getPotentiallyConvertedSimpleRead ,
299299 projection );
300300
@@ -377,11 +377,11 @@ class ProjectingConversionContext extends ConversionContext {
377377
378378 private final EntityProjection <?, ?> returnedTypeDescriptor ;
379379
380- ProjectingConversionContext (CustomConversions customConversions , ObjectPath path ,
380+ ProjectingConversionContext (MongoConverter sourceConverter , CustomConversions customConversions , ObjectPath path ,
381381 ContainerValueConverter <Collection <?>> collectionConverter , ContainerValueConverter <Bson > mapConverter ,
382382 ContainerValueConverter <DBRef > dbRefConverter , ValueConverter <Object > elementConverter ,
383383 EntityProjection <?, ?> projection ) {
384- super (customConversions , path ,
384+ super (sourceConverter , customConversions , path ,
385385 (context , source , typeHint ) -> doReadOrProject (context , source , typeHint , projection ),
386386
387387 collectionConverter , mapConverter , dbRefConverter , elementConverter );
@@ -397,13 +397,13 @@ public ConversionContext forProperty(String name) {
397397 mapConverter , dbRefConverter , elementConverter );
398398 }
399399
400- return new ProjectingConversionContext (conversions , path , collectionConverter , mapConverter , dbRefConverter ,
400+ return new ProjectingConversionContext (sourceConverter , conversions , path , collectionConverter , mapConverter , dbRefConverter ,
401401 elementConverter , property );
402402 }
403403
404404 @ Override
405405 public ConversionContext withPath (ObjectPath currentPath ) {
406- return new ProjectingConversionContext (conversions , currentPath , collectionConverter , mapConverter ,
406+ return new ProjectingConversionContext (sourceConverter , conversions , currentPath , collectionConverter , mapConverter ,
407407 dbRefConverter , elementConverter , returnedTypeDescriptor );
408408 }
409409 }
@@ -935,6 +935,11 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
935935 TypeInformation <?> valueType = ClassTypeInformation .from (obj .getClass ());
936936 TypeInformation <?> type = prop .getTypeInformation ();
937937
938+ if (conversions .hasPropertyValueConverter (prop )) {
939+ accessor .put (prop , conversions .getPropertyValueConverter (prop ).write (obj , new MongoConversionContext (prop , this )));
940+ return ;
941+ }
942+
938943 if (prop .isUnwrapped ()) {
939944
940945 Document target = new Document ();
@@ -1264,6 +1269,12 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)
12641269
12651270 private void writeSimpleInternal (@ Nullable Object value , Bson bson , MongoPersistentProperty property ) {
12661271 DocumentAccessor accessor = new DocumentAccessor (bson );
1272+
1273+ if (conversions .hasPropertyValueConverter (property )) {
1274+ accessor .put (property , conversions .getPropertyValueConverter (property ).write (value , new MongoConversionContext (property , this )));
1275+ return ;
1276+ }
1277+
12671278 accessor .put (property , getPotentiallyConvertedSimpleWrite (value ,
12681279 property .hasExplicitWriteTarget () ? property .getFieldType () : Object .class ));
12691280 }
@@ -1905,6 +1916,11 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19051916 return null ;
19061917 }
19071918
1919+ if (context .conversions .hasPropertyValueConverter (property )) {
1920+
1921+ return (T ) context .conversions .getPropertyValueConverter (property ).read (value , new MongoConversionContext (property , context .sourceConverter ));
1922+ }
1923+
19081924 return (T ) context .convert (value , property .getTypeInformation ());
19091925 }
19101926
@@ -2123,6 +2139,7 @@ public org.springframework.data.util.TypeInformation<? extends S> specialize(Cla
21232139 */
21242140 protected static class ConversionContext {
21252141
2142+ final MongoConverter sourceConverter ;
21262143 final org .springframework .data .convert .CustomConversions conversions ;
21272144 final ObjectPath path ;
21282145 final ContainerValueConverter <Bson > documentConverter ;
@@ -2131,11 +2148,12 @@ protected static class ConversionContext {
21312148 final ContainerValueConverter <DBRef > dbRefConverter ;
21322149 final ValueConverter <Object > elementConverter ;
21332150
2134- ConversionContext (org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
2151+ ConversionContext (MongoConverter sourceConverter , org .springframework .data .convert .CustomConversions customConversions , ObjectPath path ,
21352152 ContainerValueConverter <Bson > documentConverter , ContainerValueConverter <Collection <?>> collectionConverter ,
21362153 ContainerValueConverter <Bson > mapConverter , ContainerValueConverter <DBRef > dbRefConverter ,
21372154 ValueConverter <Object > elementConverter ) {
21382155
2156+ this .sourceConverter = sourceConverter ;
21392157 this .conversions = customConversions ;
21402158 this .path = path ;
21412159 this .documentConverter = documentConverter ;
@@ -2217,7 +2235,7 @@ public ConversionContext withPath(ObjectPath currentPath) {
22172235
22182236 Assert .notNull (currentPath , "ObjectPath must not be null" );
22192237
2220- return new ConversionContext (conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
2238+ return new ConversionContext (sourceConverter , conversions , currentPath , documentConverter , collectionConverter , mapConverter ,
22212239 dbRefConverter , elementConverter );
22222240 }
22232241
0 commit comments