@@ -449,74 +449,14 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
449
449
if (documentField .getProperty () != null
450
450
&& converter .getCustomConversions ().hasValueConverter (documentField .getProperty ())) {
451
451
452
- MongoConversionContext conversionContext = new MongoConversionContext (new PropertyValueProvider <>() {
453
- @ Override
454
- public <T > T getPropertyValue (MongoPersistentProperty property ) {
455
- throw new IllegalStateException ("No enclosing property available" );
456
- }
457
- }, documentField .getProperty (), converter );
458
452
PropertyValueConverter <Object , Object , ValueConversionContext <MongoPersistentProperty >> valueConverter = converter
459
453
.getCustomConversions ().getPropertyValueConversions ().getValueConverter (documentField .getProperty ());
460
454
461
- /* might be an $in clause with multiple entries */
462
- if (!documentField .getProperty ().isCollectionLike () && sourceValue instanceof Collection <?> collection ) {
463
- return collection .stream ().map (it -> valueConverter .write (it , conversionContext )).collect (Collectors .toList ());
464
- }
465
-
466
- if (!documentField .getProperty ().isMap () && sourceValue instanceof Document document ) {
467
- return new Document (document .entrySet ().stream ().collect (Collectors .toMap (entry -> entry .getKey (), entry -> {
468
- if (isKeyword (entry .getKey ())) {
469
- return getMappedValue (documentField , entry .getValue ());
470
- }
471
- return entry .getValue ();
472
- })));
473
- }
474
-
475
- return valueConverter .write (value , conversionContext );
455
+ return convertValue (documentField , sourceValue , value , valueConverter );
476
456
}
477
457
478
458
if (documentField .isIdField () && !documentField .isAssociation ()) {
479
-
480
- if (isDBObject (value )) {
481
- DBObject valueDbo = (DBObject ) value ;
482
- Document resultDbo = new Document (valueDbo .toMap ());
483
-
484
- if (valueDbo .containsField ("$in" ) || valueDbo .containsField ("$nin" )) {
485
- String inKey = valueDbo .containsField ("$in" ) ? "$in" : "$nin" ;
486
- List <Object > ids = new ArrayList <>();
487
- for (Object id : (Iterable <?>) valueDbo .get (inKey )) {
488
- ids .add (convertId (id , getIdTypeForField (documentField )));
489
- }
490
- resultDbo .put (inKey , ids );
491
- } else if (valueDbo .containsField ("$ne" )) {
492
- resultDbo .put ("$ne" , convertId (valueDbo .get ("$ne" ), getIdTypeForField (documentField )));
493
- } else {
494
- return getMappedObject (resultDbo , Optional .empty ());
495
- }
496
- return resultDbo ;
497
- }
498
-
499
- else if (isDocument (value )) {
500
- Document valueDbo = (Document ) value ;
501
- Document resultDbo = new Document (valueDbo );
502
-
503
- if (valueDbo .containsKey ("$in" ) || valueDbo .containsKey ("$nin" )) {
504
- String inKey = valueDbo .containsKey ("$in" ) ? "$in" : "$nin" ;
505
- List <Object > ids = new ArrayList <>();
506
- for (Object id : (Iterable <?>) valueDbo .get (inKey )) {
507
- ids .add (convertId (id , getIdTypeForField (documentField )));
508
- }
509
- resultDbo .put (inKey , ids );
510
- } else if (valueDbo .containsKey ("$ne" )) {
511
- resultDbo .put ("$ne" , convertId (valueDbo .get ("$ne" ), getIdTypeForField (documentField )));
512
- } else {
513
- return getMappedObject (resultDbo , Optional .empty ());
514
- }
515
- return resultDbo ;
516
-
517
- } else {
518
- return convertId (value , getIdTypeForField (documentField ));
519
- }
459
+ return convertIdField (documentField , value );
520
460
}
521
461
522
462
if (value == null ) {
@@ -717,6 +657,66 @@ protected Object convertAssociation(@Nullable Object source, @Nullable MongoPers
717
657
return createReferenceFor (source , property );
718
658
}
719
659
660
+ @ Nullable
661
+ private Object convertValue (Field documentField , Object sourceValue , Object value ,
662
+ PropertyValueConverter <Object , Object , ValueConversionContext <MongoPersistentProperty >> valueConverter ) {
663
+
664
+ MongoConversionContext conversionContext = new MongoConversionContext (new PropertyValueProvider <>() {
665
+ @ Override
666
+ public <T > T getPropertyValue (MongoPersistentProperty property ) {
667
+ throw new IllegalStateException ("No enclosing property available" );
668
+ }
669
+ }, documentField .getProperty (), converter );
670
+
671
+ /* might be an $in clause with multiple entries */
672
+ if (!documentField .getProperty ().isCollectionLike () && sourceValue instanceof Collection <?> collection ) {
673
+ return collection .stream ().map (it -> valueConverter .write (it , conversionContext )).collect (Collectors .toList ());
674
+ }
675
+
676
+ if (!documentField .getProperty ().isMap () && sourceValue instanceof Document document ) {
677
+ return new Document (document .entrySet ().stream ().collect (Collectors .toMap (Entry ::getKey , entry -> {
678
+ if (isKeyword (entry .getKey ())) {
679
+ return getMappedValue (documentField , entry .getValue ());
680
+ }
681
+ return entry .getValue ();
682
+ })));
683
+ }
684
+
685
+ return valueConverter .write (value , conversionContext );
686
+ }
687
+
688
+ @ Nullable
689
+ private Object convertIdField (Field documentField , Object source ) {
690
+
691
+ Object value = source ;
692
+ if (isDBObject (source )) {
693
+ DBObject valueDbo = (DBObject ) source ;
694
+ value = new Document (valueDbo .toMap ());
695
+ }
696
+
697
+ if (!isDocument (value )) {
698
+ return convertId (value , getIdTypeForField (documentField ));
699
+ }
700
+
701
+ Document valueDbo = (Document ) value ;
702
+ Document resultDbo = new Document (valueDbo );
703
+
704
+ if (valueDbo .containsKey ("$in" ) || valueDbo .containsKey ("$nin" )) {
705
+ String inKey = valueDbo .containsKey ("$in" ) ? "$in" : "$nin" ;
706
+ List <Object > ids = new ArrayList <>();
707
+ for (Object id : (Iterable <?>) valueDbo .get (inKey )) {
708
+ ids .add (convertId (id , getIdTypeForField (documentField )));
709
+ }
710
+ resultDbo .put (inKey , ids );
711
+ } else if (valueDbo .containsKey ("$ne" )) {
712
+ resultDbo .put ("$ne" , convertId (valueDbo .get ("$ne" ), getIdTypeForField (documentField )));
713
+ } else {
714
+ return getMappedObject (resultDbo , Optional .empty ());
715
+ }
716
+ return resultDbo ;
717
+
718
+ }
719
+
720
720
/**
721
721
* Checks whether the given value is a {@link Document}.
722
722
*
0 commit comments