26
26
import java .util .Date ;
27
27
import java .util .List ;
28
28
29
+ import org .bson .BsonBinary ;
29
30
import org .bson .Document ;
30
31
import org .bson .codecs .DecoderContext ;
31
32
import org .junit .jupiter .api .Test ;
@@ -347,7 +348,7 @@ void evaluatesSpelExpressionDefiningEntireQuery() {
347
348
evaluationContext .setRootObject (new DummySecurityObject (new DummyWithId ("wonderwoman" )));
348
349
349
350
String json = "?#{ T(" + this .getClass ().getName ()
350
- + ").isBatman() ? \" {'_class': { '$eq' : 'region' }}\" : \" { '$and' : [ {'_class': { '$eq' : 'region' } }, {'user.supervisor': ' \" + principal.id + \" ' } ] } \" }" ;
351
+ + ").isBatman() ? {'_class': { '$eq' : 'region' }} : { '$and' : { {'_class': { '$eq' : 'region' } }, {'user.supervisor': principal.id } } } }" ;
351
352
352
353
ParameterBindingJsonReader reader = new ParameterBindingJsonReader (json ,
353
354
new ParameterBindingContext ((index ) -> args [index ], new SpelExpressionParser (), evaluationContext ));
@@ -357,11 +358,66 @@ void evaluatesSpelExpressionDefiningEntireQuery() {
357
358
.isEqualTo (new Document ("$and" , Arrays .asList (new Document ("_class" , new Document ("$eq" , "region" )),
358
359
new Document ("user.supervisor" , "wonderwoman" ))));
359
360
}
360
-
361
- @ Test
361
+
362
+ @ Test // GH-3871
363
+ public void capturingExpressionDependenciesShouldNotThrowParseErrorForSpelOnlyJson () {
364
+
365
+ Object [] args = new Object [] { "1" , "2" };
366
+ String json = "?#{ true ? { 'name': #name } : { 'name' : #name + 'trouble' } }" ;
367
+
368
+ new ParameterBindingDocumentCodec ().captureExpressionDependencies (json , (index ) -> args [index ],
369
+ new SpelExpressionParser ());
370
+ }
371
+
372
+ @ Test // GH-3871
373
+ public void bindEntireQueryUsingSpelExpressionWhenEvaluationResultIsJsonString () {
374
+
375
+ Object [] args = new Object [] { "expected" , "unexpected" };
376
+ String json = "?#{ true ? \" { 'name': ?0 }\" : \" { 'name' : ?1 }\" }" ;
377
+ StandardEvaluationContext evaluationContext = (StandardEvaluationContext ) EvaluationContextProvider .DEFAULT
378
+ .getEvaluationContext (args );
379
+
380
+ ParameterBindingJsonReader reader = new ParameterBindingJsonReader (json ,
381
+ new ParameterBindingContext ((index ) -> args [index ], new SpelExpressionParser (), evaluationContext ));
382
+
383
+ Document target = new ParameterBindingDocumentCodec ().decode (reader , DecoderContext .builder ().build ());
384
+ assertThat (target ).isEqualTo (new Document ("name" , "expected" ));
385
+ }
386
+
387
+ @ Test // GH-3871
388
+ public void throwsExceptionWhenbindEntireQueryUsingSpelExpressionResultsInInvalidJsonString () {
389
+
390
+ Object [] args = new Object [] { "expected" , "unexpected" };
391
+ String json = "?#{ true ? \" { 'name': ?0 { }\" : \" { 'name' : ?1 }\" }" ;
392
+ StandardEvaluationContext evaluationContext = (StandardEvaluationContext ) EvaluationContextProvider .DEFAULT
393
+ .getEvaluationContext (args );
394
+
395
+ ParameterBindingJsonReader reader = new ParameterBindingJsonReader (json ,
396
+ new ParameterBindingContext ((index ) -> args [index ], new SpelExpressionParser (), evaluationContext ));
397
+
398
+ assertThatExceptionOfType (IllegalArgumentException .class ).isThrownBy (() -> new ParameterBindingDocumentCodec ().decode (reader , DecoderContext .builder ().build ()));
399
+ }
400
+
401
+ @ Test // GH-3871
402
+ public void bindEntireQueryUsingSpelExpressionWhenEvaluationResultIsJsonStringContainingUUID () {
403
+
404
+ Object [] args = new Object [] { "UUID('cfbca728-4e39-4613-96bc-f920b5c37e16')" , "unexpected" };
405
+ String json = "?#{ true ? \" { 'name': ?0 }\" : \" { 'name' : ?1 }\" }" ;
406
+ StandardEvaluationContext evaluationContext = (StandardEvaluationContext ) EvaluationContextProvider .DEFAULT
407
+ .getEvaluationContext (args );
408
+
409
+ ParameterBindingJsonReader reader = new ParameterBindingJsonReader (json ,
410
+ new ParameterBindingContext ((index ) -> args [index ], new SpelExpressionParser (), evaluationContext ));
411
+
412
+ Document target = new ParameterBindingDocumentCodec ().decode (reader , DecoderContext .builder ().build ());
413
+
414
+ assertThat (target .get ("name" )).isInstanceOf (BsonBinary .class );
415
+ }
416
+
417
+ @ Test // GH-3871
362
418
void bindEntireQueryUsingSpelExpression () {
363
419
364
- Object [] args = new Object [] {"region" };
420
+ Object [] args = new Object [] { "region" };
365
421
StandardEvaluationContext evaluationContext = (StandardEvaluationContext ) EvaluationContextProvider .DEFAULT
366
422
.getEvaluationContext (args );
367
423
evaluationContext .setRootObject (new DummySecurityObject (new DummyWithId ("wonderwoman" )));
@@ -377,10 +433,10 @@ void bindEntireQueryUsingSpelExpression() {
377
433
new Document ("user.supervisor" , "wonderwoman" ))));
378
434
}
379
435
380
- @ Test
436
+ @ Test // GH-3871
381
437
void bindEntireQueryUsingParameter () {
382
438
383
- Object [] args = new Object [] {"{ 'itWorks' : true }" };
439
+ Object [] args = new Object [] { "{ 'itWorks' : true }" };
384
440
StandardEvaluationContext evaluationContext = (StandardEvaluationContext ) EvaluationContextProvider .DEFAULT
385
441
.getEvaluationContext (args );
386
442
@@ -390,9 +446,7 @@ void bindEntireQueryUsingParameter() {
390
446
new ParameterBindingContext ((index ) -> args [index ], new SpelExpressionParser (), evaluationContext ));
391
447
Document target = new ParameterBindingDocumentCodec ().decode (reader , DecoderContext .builder ().build ());
392
448
393
- assertThat (target )
394
- .isEqualTo (new Document ("itWorks" , true ));
395
-
449
+ assertThat (target ).isEqualTo (new Document ("itWorks" , true ));
396
450
}
397
451
398
452
@ Test // DATAMONGO-2571
@@ -437,17 +491,13 @@ private static Document parse(String json, Object... args) {
437
491
public static boolean isBatman () {
438
492
return false ;
439
493
}
440
-
494
+
441
495
public static String applyFilterByUser (String _class , String username ) {
442
496
switch (username ) {
443
497
case "batman" :
444
- return "{'_class': { '$eq' : '"
445
- + _class
446
- + "' }}" ;
498
+ return "{'_class': { '$eq' : '" + _class + "' }}" ;
447
499
default :
448
- return "{ '$and' : [ {'_class': { '$eq' : '"
449
- + _class
450
- + "' } }, {'user.supervisor': '" + username + "' } ] }" ;
500
+ return "{ '$and' : [ {'_class': { '$eq' : '" + _class + "' } }, {'user.supervisor': '" + username + "' } ] }" ;
451
501
}
452
502
}
453
503
0 commit comments