1515 */
1616package org .springframework .data .mongodb .util .json ;
1717
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 ;
1920
2021import java .nio .charset .StandardCharsets ;
2122import java .util .Arrays ;
3031import org .bson .Document ;
3132import org .bson .codecs .DecoderContext ;
3233import org .junit .jupiter .api .Test ;
33-
3434import org .springframework .data .expression .ValueExpressionParser ;
3535import org .springframework .data .spel .EvaluationContextProvider ;
3636import org .springframework .data .spel .ExpressionDependencies ;
@@ -104,6 +104,22 @@ void regexConsidersBindValueWithOptions() {
104104 assertThat (pattern .getOptions ()).isEqualTo ("i" );
105105 }
106106
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+
107123 @ Test
108124 void bindValueToRegex () {
109125
@@ -147,15 +163,13 @@ void bindToKey() {
147163 @ Test
148164 void bindListValue () {
149165
150- //
151166 Document target = parse ("{ 'lastname' : { $in : ?0 } }" , Arrays .asList ("Kohlin" , "Davar" ));
152167 assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ));
153168 }
154169
155170 @ Test
156171 void bindListOfBinaryValue () {
157172
158- //
159173 byte [] value = "Kohlin" .getBytes (StandardCharsets .UTF_8 );
160174 List <byte []> args = Collections .singletonList (value );
161175
@@ -170,28 +184,23 @@ void bindExtendedExpression() {
170184 assertThat (target ).isEqualTo (Document .parse ("{ \" id\" : { \" $exists\" : true}}" ));
171185 }
172186
173- // {'id':?#{ [0] ? { $exists :true} : [1] }}
174-
175187 @ Test
176188 void bindDocumentValue () {
177189
178- //
179190 Document target = parse ("{ 'lastname' : ?0 }" , new Document ("$eq" , "Kohlin" ));
180191 assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { '$eq' : 'Kohlin' } }" ));
181192 }
182193
183194 @ Test
184195 void arrayWithoutBinding () {
185196
186- //
187197 Document target = parse ("{ 'lastname' : { $in : [\" Kohlin\" , \" Davar\" ] } }" );
188198 assertThat (target ).isEqualTo (Document .parse ("{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ));
189199 }
190200
191201 @ Test
192202 void bindSpEL () {
193203
194- // "{ arg0 : ?#{[0]} }"
195204 Document target = parse ("{ arg0 : ?#{[0]} }" , 100.01D );
196205 assertThat (target ).isEqualTo (new Document ("arg0" , 100.01D ));
197206 }
@@ -331,9 +340,8 @@ void discoversNoDependenciesInExpression() {
331340
332341 String json = "{ $and : [?#{ [0] == null ? { '$where' : 'true' } : { 'v1' : { '$in' : {[0]} } } }]}" ;
333342
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 ));
337345
338346 assertThat (expressionDependencies ).isEqualTo (ExpressionDependencies .none ());
339347 }
@@ -343,9 +351,8 @@ void discoversCorrectlyDependenciesInExpression() {
343351
344352 String json = "{ hello: ?#{hasRole('foo')} }" ;
345353
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 ));
349356
350357 assertThat (expressionDependencies ).isNotEmpty ();
351358 assertThat (expressionDependencies .get ()).hasSize (1 );
0 commit comments