17
17
18
18
import java .util .Collection ;
19
19
import java .util .LinkedHashMap ;
20
+ import java .util .Map ;
20
21
21
22
import org .apache .commons .logging .Log ;
22
23
import org .apache .commons .logging .LogFactory ;
25
26
import org .bson .BsonDocument ;
26
27
import org .bson .BsonValue ;
27
28
import org .bson .Document ;
29
+ import org .bson .conversions .Bson ;
28
30
import org .bson .types .Binary ;
29
31
import org .springframework .core .CollectionFactory ;
30
32
import org .springframework .data .mongodb .core .convert .MongoConversionContext ;
@@ -63,7 +65,7 @@ public MongoEncryptionConverter(Encryption<BsonValue, BsonBinary> encryption, En
63
65
public Object read (Object value , MongoConversionContext context ) {
64
66
65
67
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 ;
67
69
}
68
70
69
71
@ Override
@@ -87,36 +89,56 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
87
89
}
88
90
89
91
MongoPersistentProperty persistentProperty = getProperty (context );
90
-
91
92
if (getProperty (context ).isCollectionLike () && decryptedValue instanceof Iterable <?> iterable ) {
92
93
93
94
int size = iterable instanceof Collection <?> c ? c .size () : 10 ;
94
95
95
96
if (!persistentProperty .isEntity ()) {
96
97
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
+
98
106
return collection ;
99
107
} else {
100
108
Collection <Object > collection = CollectionFactory .createCollection (persistentProperty .getType (), size );
101
109
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
+ }
103
115
});
104
116
return collection ;
105
117
}
106
118
}
107
119
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
+ }
112
131
}
113
- return BsonUtils .toJavaType (bsonValue );
114
132
}
115
133
116
134
if (persistentProperty .isEntity () && decryptedValue instanceof BsonDocument bsonDocument ) {
117
135
return context .read (BsonUtils .toJavaType (bsonDocument ), persistentProperty .getTypeInformation ().getType ());
118
136
}
119
137
138
+ if (persistentProperty .isEntity () && decryptedValue instanceof Document document ) {
139
+ return context .read (document , persistentProperty .getTypeInformation ().getType ());
140
+ }
141
+
120
142
return decryptedValue ;
121
143
}
122
144
0 commit comments