15
15
*/
16
16
package org .springframework .data .mongodb .util .json ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
18
+ import static org .assertj .core .api .Assertions .assertThat ;
19
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
19
20
20
21
import java .nio .charset .StandardCharsets ;
21
22
import java .util .Arrays ;
30
31
import org .bson .Document ;
31
32
import org .bson .codecs .DecoderContext ;
32
33
import org .junit .jupiter .api .Test ;
33
-
34
34
import org .springframework .data .expression .ValueExpressionParser ;
35
35
import org .springframework .data .spel .EvaluationContextProvider ;
36
36
import org .springframework .data .spel .ExpressionDependencies ;
@@ -104,6 +104,22 @@ void regexConsidersBindValueWithOptions() {
104
104
assertThat (pattern .getOptions ()).isEqualTo ("i" );
105
105
}
106
106
107
+ @ Test // GH-4806
108
+ void treatsQuotedValueThatLooksLikeRegexAsPlainString () {
109
+
110
+ Document target = parse ("{ 'c': '/^?0$/i' }" , "foo" );
111
+
112
+ assertThat (target .get ("c" )).isInstanceOf (String .class );
113
+ }
114
+
115
+ @ Test // GH-4806
116
+ void treatsStringParameterValueThatLooksLikeRegexAsPlainString () {
117
+
118
+ Document target = parse ("{ 'c': ?0 }" , "/^foo$/i" );
119
+
120
+ assertThat (target .get ("c" )).isInstanceOf (String .class );
121
+ }
122
+
107
123
@ Test
108
124
void bindValueToRegex () {
109
125
@@ -147,15 +163,13 @@ void bindToKey() {
147
163
@ Test
148
164
void bindListValue () {
149
165
150
- //
151
166
Document target = parse ("{ 'lastname' : { $in : ?0 } }" , Arrays .asList ("Kohlin" , "Davar" ));
152
167
assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ));
153
168
}
154
169
155
170
@ Test
156
171
void bindListOfBinaryValue () {
157
172
158
- //
159
173
byte [] value = "Kohlin" .getBytes (StandardCharsets .UTF_8 );
160
174
List <byte []> args = Collections .singletonList (value );
161
175
@@ -170,28 +184,23 @@ void bindExtendedExpression() {
170
184
assertThat (target ).isEqualTo (Document .parse ("{ \" id\" : { \" $exists\" : true}}" ));
171
185
}
172
186
173
- // {'id':?#{ [0] ? { $exists :true} : [1] }}
174
-
175
187
@ Test
176
188
void bindDocumentValue () {
177
189
178
- //
179
190
Document target = parse ("{ 'lastname' : ?0 }" , new Document ("$eq" , "Kohlin" ));
180
191
assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { '$eq' : 'Kohlin' } }" ));
181
192
}
182
193
183
194
@ Test
184
195
void arrayWithoutBinding () {
185
196
186
- //
187
197
Document target = parse ("{ 'lastname' : { $in : [\" Kohlin\" , \" Davar\" ] } }" );
188
198
assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ));
189
199
}
190
200
191
201
@ Test
192
202
void bindSpEL () {
193
203
194
- // "{ arg0 : ?#{[0]} }"
195
204
Document target = parse ("{ arg0 : ?#{[0]} }" , 100.01D );
196
205
assertThat (target ).isEqualTo (new Document ("arg0" , 100.01D ));
197
206
}
@@ -331,9 +340,8 @@ void discoversNoDependenciesInExpression() {
331
340
332
341
String json = "{ $and : [?#{ [0] == null ? { '$where' : 'true' } : { 'v1' : { '$in' : {[0]} } } }]}" ;
333
342
334
- ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ()
335
- .captureExpressionDependencies (json , it -> new Object (),
336
- ValueExpressionParser .create (SpelExpressionParser ::new ));
343
+ ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ().captureExpressionDependencies (
344
+ json , it -> new Object (), ValueExpressionParser .create (SpelExpressionParser ::new ));
337
345
338
346
assertThat (expressionDependencies ).isEqualTo (ExpressionDependencies .none ());
339
347
}
@@ -343,9 +351,8 @@ void discoversCorrectlyDependenciesInExpression() {
343
351
344
352
String json = "{ hello: ?#{hasRole('foo')} }" ;
345
353
346
- ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ()
347
- .captureExpressionDependencies (json , it -> new Object (),
348
- ValueExpressionParser .create (SpelExpressionParser ::new ));
354
+ ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ().captureExpressionDependencies (
355
+ json , it -> new Object (), ValueExpressionParser .create (SpelExpressionParser ::new ));
349
356
350
357
assertThat (expressionDependencies ).isNotEmpty ();
351
358
assertThat (expressionDependencies .get ()).hasSize (1 );
0 commit comments