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