@ -15,7 +15,8 @@
* /
* /
package org.springframework.data.mongodb.util.json ;
package org.springframework.data.mongodb.util.json ;
import static org.assertj.core.api.Assertions.* ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import java.nio.charset.StandardCharsets ;
import java.nio.charset.StandardCharsets ;
import java.util.Arrays ;
import java.util.Arrays ;
@ -30,7 +31,6 @@ import org.bson.BsonRegularExpression;
import org.bson.Document ;
import org.bson.Document ;
import org.bson.codecs.DecoderContext ;
import org.bson.codecs.DecoderContext ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Test ;
import org.springframework.data.expression.ValueExpressionParser ;
import org.springframework.data.expression.ValueExpressionParser ;
import org.springframework.data.spel.EvaluationContextProvider ;
import org.springframework.data.spel.EvaluationContextProvider ;
import org.springframework.data.spel.ExpressionDependencies ;
import org.springframework.data.spel.ExpressionDependencies ;
@ -104,6 +104,22 @@ class ParameterBindingJsonReaderUnitTests {
assertThat ( pattern . getOptions ( ) ) . isEqualTo ( "i" ) ;
assertThat ( pattern . getOptions ( ) ) . isEqualTo ( "i" ) ;
}
}
@Test // GH-4806
void treatsQuotedValueThatLooksLikeRegexAsPlainString ( ) {
Document target = parse ( "{ 'c': '/^?0$/i' }" , "foo" ) ;
assertThat ( target . get ( "c" ) ) . isInstanceOf ( String . class ) ;
}
@Test // GH-4806
void treatsStringParameterValueThatLooksLikeRegexAsPlainString ( ) {
Document target = parse ( "{ 'c': ?0 }" , "/^foo$/i" ) ;
assertThat ( target . get ( "c" ) ) . isInstanceOf ( String . class ) ;
}
@Test
@Test
void bindValueToRegex ( ) {
void bindValueToRegex ( ) {
@ -147,7 +163,6 @@ class ParameterBindingJsonReaderUnitTests {
@Test
@Test
void bindListValue ( ) {
void bindListValue ( ) {
//
Document target = parse ( "{ 'lastname' : { $in : ?0 } }" , Arrays . asList ( "Kohlin" , "Davar" ) ) ;
Document target = parse ( "{ 'lastname' : { $in : ?0 } }" , Arrays . asList ( "Kohlin" , "Davar" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ) ) ;
}
}
@ -155,7 +170,6 @@ class ParameterBindingJsonReaderUnitTests {
@Test
@Test
void bindListOfBinaryValue ( ) {
void bindListOfBinaryValue ( ) {
//
byte [ ] value = "Kohlin" . getBytes ( StandardCharsets . UTF_8 ) ;
byte [ ] value = "Kohlin" . getBytes ( StandardCharsets . UTF_8 ) ;
List < byte [ ] > args = Collections . singletonList ( value ) ;
List < byte [ ] > args = Collections . singletonList ( value ) ;
@ -170,12 +184,9 @@ class ParameterBindingJsonReaderUnitTests {
assertThat ( target ) . isEqualTo ( Document . parse ( "{ \"id\" : { \"$exists\" : true}}" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ \"id\" : { \"$exists\" : true}}" ) ) ;
}
}
// {'id':?#{ [0] ? { $exists :true} : [1] }}
@Test
@Test
void bindDocumentValue ( ) {
void bindDocumentValue ( ) {
//
Document target = parse ( "{ 'lastname' : ?0 }" , new Document ( "$eq" , "Kohlin" ) ) ;
Document target = parse ( "{ 'lastname' : ?0 }" , new Document ( "$eq" , "Kohlin" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { '$eq' : 'Kohlin' } }" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { '$eq' : 'Kohlin' } }" ) ) ;
}
}
@ -183,7 +194,6 @@ class ParameterBindingJsonReaderUnitTests {
@Test
@Test
void arrayWithoutBinding ( ) {
void arrayWithoutBinding ( ) {
//
Document target = parse ( "{ 'lastname' : { $in : [\"Kohlin\", \"Davar\"] } }" ) ;
Document target = parse ( "{ 'lastname' : { $in : [\"Kohlin\", \"Davar\"] } }" ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ) ) ;
assertThat ( target ) . isEqualTo ( Document . parse ( "{ 'lastname' : { $in : ['Kohlin', 'Davar' ]} }" ) ) ;
}
}
@ -191,7 +201,6 @@ class ParameterBindingJsonReaderUnitTests {
@Test
@Test
void bindSpEL ( ) {
void bindSpEL ( ) {
// "{ arg0 : ?#{[0]} }"
Document target = parse ( "{ arg0 : ?#{[0]} }" , 100 . 01D ) ;
Document target = parse ( "{ arg0 : ?#{[0]} }" , 100 . 01D ) ;
assertThat ( target ) . isEqualTo ( new Document ( "arg0" , 100 . 01D ) ) ;
assertThat ( target ) . isEqualTo ( new Document ( "arg0" , 100 . 01D ) ) ;
}
}
@ -331,9 +340,8 @@ class ParameterBindingJsonReaderUnitTests {
String json = "{ $and : [?#{ [0] == null ? { '$where' : 'true' } : { 'v1' : { '$in' : {[0]} } } }]}" ;
String json = "{ $and : [?#{ [0] == null ? { '$where' : 'true' } : { 'v1' : { '$in' : {[0]} } } }]}" ;
ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ( )
ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ( ) . captureExpressionDependencies (
. captureExpressionDependencies ( json , it - > new Object ( ) ,
json , it - > new Object ( ) , ValueExpressionParser . create ( SpelExpressionParser : : new ) ) ;
ValueExpressionParser . create ( SpelExpressionParser : : new ) ) ;
assertThat ( expressionDependencies ) . isEqualTo ( ExpressionDependencies . none ( ) ) ;
assertThat ( expressionDependencies ) . isEqualTo ( ExpressionDependencies . none ( ) ) ;
}
}
@ -343,9 +351,8 @@ class ParameterBindingJsonReaderUnitTests {
String json = "{ hello: ?#{hasRole('foo')} }" ;
String json = "{ hello: ?#{hasRole('foo')} }" ;
ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ( )
ExpressionDependencies expressionDependencies = new ParameterBindingDocumentCodec ( ) . captureExpressionDependencies (
. captureExpressionDependencies ( json , it - > new Object ( ) ,
json , it - > new Object ( ) , ValueExpressionParser . create ( SpelExpressionParser : : new ) ) ;
ValueExpressionParser . create ( SpelExpressionParser : : new ) ) ;
assertThat ( expressionDependencies ) . isNotEmpty ( ) ;
assertThat ( expressionDependencies ) . isNotEmpty ( ) ;
assertThat ( expressionDependencies . get ( ) ) . hasSize ( 1 ) ;
assertThat ( expressionDependencies . get ( ) ) . hasSize ( 1 ) ;