1717
1818import java .util .Collection ;
1919import java .util .LinkedHashMap ;
20+ import java .util .Map ;
2021
2122import org .apache .commons .logging .Log ;
2223import org .apache .commons .logging .LogFactory ;
2526import org .bson .BsonDocument ;
2627import org .bson .BsonValue ;
2728import org .bson .Document ;
29+ import org .bson .conversions .Bson ;
2830import org .bson .types .Binary ;
2931import org .springframework .core .CollectionFactory ;
3032import org .springframework .data .mongodb .core .convert .MongoConversionContext ;
@@ -63,7 +65,7 @@ public MongoEncryptionConverter(Encryption<BsonValue, BsonBinary> encryption, En
6365 public Object read (Object value , MongoConversionContext context ) {
6466
6567 Object decrypted = EncryptingConverter .super .read (value , context );
66- return decrypted instanceof BsonValue ? BsonUtils .toJavaType (( BsonValue ) decrypted ) : decrypted ;
68+ return decrypted instanceof BsonValue bsonValue ? BsonUtils .toJavaType (bsonValue ) : decrypted ;
6769 }
6870
6971 @ Override
@@ -87,36 +89,56 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
8789 }
8890
8991 MongoPersistentProperty persistentProperty = getProperty (context );
90-
9192 if (getProperty (context ).isCollectionLike () && decryptedValue instanceof Iterable <?> iterable ) {
9293
9394 int size = iterable instanceof Collection <?> c ? c .size () : 10 ;
9495
9596 if (!persistentProperty .isEntity ()) {
9697 Collection <Object > collection = CollectionFactory .createCollection (persistentProperty .getType (), size );
97- iterable .forEach (it -> collection .add (BsonUtils .toJavaType ((BsonValue ) it )));
98+ iterable .forEach (it -> {
99+ if (it instanceof BsonValue bsonValue ) {
100+ collection .add (BsonUtils .toJavaType (bsonValue ));
101+ } else {
102+ collection .add (context .read (it , persistentProperty .getActualType ()));
103+ }
104+ });
105+
98106 return collection ;
99107 } else {
100108 Collection <Object > collection = CollectionFactory .createCollection (persistentProperty .getType (), size );
101109 iterable .forEach (it -> {
102- collection .add (context .read (BsonUtils .toJavaType ((BsonValue ) it ), persistentProperty .getActualType ()));
110+ if (it instanceof BsonValue bsonValue ) {
111+ collection .add (context .read (BsonUtils .toJavaType (bsonValue ), persistentProperty .getActualType ()));
112+ } else {
113+ collection .add (context .read (it , persistentProperty .getActualType ()));
114+ }
103115 });
104116 return collection ;
105117 }
106118 }
107119
108- if (!persistentProperty .isEntity () && decryptedValue instanceof BsonValue bsonValue ) {
109- if (persistentProperty .isMap () && persistentProperty .getType () != Document .class ) {
110- return new LinkedHashMap <>((Document ) BsonUtils .toJavaType (bsonValue ));
111-
120+ if (!persistentProperty .isEntity () && persistentProperty .isMap ()) {
121+ if (persistentProperty .getType () != Document .class ) {
122+ if (decryptedValue instanceof BsonValue bsonValue ) {
123+ return new LinkedHashMap <>((Document ) BsonUtils .toJavaType (bsonValue ));
124+ }
125+ if (decryptedValue instanceof Document document ) {
126+ return new LinkedHashMap <>(document );
127+ }
128+ if (decryptedValue instanceof Map map ) {
129+ return map ;
130+ }
112131 }
113- return BsonUtils .toJavaType (bsonValue );
114132 }
115133
116134 if (persistentProperty .isEntity () && decryptedValue instanceof BsonDocument bsonDocument ) {
117135 return context .read (BsonUtils .toJavaType (bsonDocument ), persistentProperty .getTypeInformation ().getType ());
118136 }
119137
138+ if (persistentProperty .isEntity () && decryptedValue instanceof Document document ) {
139+ return context .read (document , persistentProperty .getTypeInformation ().getType ());
140+ }
141+
120142 return decryptedValue ;
121143 }
122144
0 commit comments