|
26 | 26 | import org.bson.Document;
|
27 | 27 | import org.bson.codecs.DecoderContext;
|
28 | 28 | import org.junit.jupiter.api.Test;
|
| 29 | +import org.springframework.data.spel.EvaluationContextProvider; |
29 | 30 | import org.springframework.expression.EvaluationContext;
|
30 | 31 | import org.springframework.expression.TypedValue;
|
31 | 32 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
@@ -264,10 +265,60 @@ void bindQuotedMulitParameterInArray() {
|
264 | 265 | assertThat(target).isEqualTo(Document.parse("{\"$and\": [{\"v1\": {\"$in\": [1]}}]}"));
|
265 | 266 | }
|
266 | 267 |
|
| 268 | + @Test // DATAMONGO-2545 |
| 269 | + void shouldABindArgumentsViaIndexInSpelExpressions() { |
| 270 | + |
| 271 | + Object[] args = new Object[] { "yess", "nooo" }; |
| 272 | + StandardEvaluationContext evaluationContext = (StandardEvaluationContext) EvaluationContextProvider.DEFAULT |
| 273 | + .getEvaluationContext(args); |
| 274 | + |
| 275 | + ParameterBindingJsonReader reader = new ParameterBindingJsonReader( |
| 276 | + "{ 'isBatman' : ?#{ T(" + this.getClass().getName() + ").isBatman() ? [0] : [1] }}", |
| 277 | + new ParameterBindingContext((index) -> args[index], new SpelExpressionParser(), evaluationContext)); |
| 278 | + Document target = new ParameterBindingDocumentCodec().decode(reader, DecoderContext.builder().build()); |
| 279 | + |
| 280 | + assertThat(target).isEqualTo(new Document("isBatman", "nooo")); |
| 281 | + } |
| 282 | + |
| 283 | + @Test // DATAMONGO-2545 |
| 284 | + void shouldAllowMethodArgumentPlaceholdersInSpelExpressions/*becuase this worked before*/() { |
| 285 | + |
| 286 | + Object[] args = new Object[] { "yess", "nooo" }; |
| 287 | + StandardEvaluationContext evaluationContext = (StandardEvaluationContext) EvaluationContextProvider.DEFAULT |
| 288 | + .getEvaluationContext(args); |
| 289 | + |
| 290 | + ParameterBindingJsonReader reader = new ParameterBindingJsonReader( |
| 291 | + "{ 'isBatman' : ?#{ T(" + this.getClass().getName() + ").isBatman() ? '?0' : '?1' }}", |
| 292 | + new ParameterBindingContext((index) -> args[index], new SpelExpressionParser(), evaluationContext)); |
| 293 | + Document target = new ParameterBindingDocumentCodec().decode(reader, DecoderContext.builder().build()); |
| 294 | + |
| 295 | + assertThat(target).isEqualTo(new Document("isBatman", "nooo")); |
| 296 | + } |
| 297 | + |
| 298 | + @Test // DATAMONGO-2545 |
| 299 | + void shouldAllowMethodArgumentPlaceholdersInQuotedSpelExpressions/*becuase this worked before*/() { |
| 300 | + |
| 301 | + Object[] args = new Object[] { "yess", "nooo" }; |
| 302 | + StandardEvaluationContext evaluationContext = (StandardEvaluationContext) EvaluationContextProvider.DEFAULT |
| 303 | + .getEvaluationContext(args); |
| 304 | + |
| 305 | + ParameterBindingJsonReader reader = new ParameterBindingJsonReader( |
| 306 | + "{ 'isBatman' : \"?#{ T(" + this.getClass().getName() + ").isBatman() ? '?0' : '?1' }\" }", |
| 307 | + new ParameterBindingContext((index) -> args[index], new SpelExpressionParser(), evaluationContext)); |
| 308 | + Document target = new ParameterBindingDocumentCodec().decode(reader, DecoderContext.builder().build()); |
| 309 | + |
| 310 | + assertThat(target).isEqualTo(new Document("isBatman", "nooo")); |
| 311 | + } |
| 312 | + |
267 | 313 | private static Document parse(String json, Object... args) {
|
268 | 314 |
|
269 | 315 | ParameterBindingJsonReader reader = new ParameterBindingJsonReader(json, args);
|
270 | 316 | return new ParameterBindingDocumentCodec().decode(reader, DecoderContext.builder().build());
|
271 | 317 | }
|
272 | 318 |
|
| 319 | + // DATAMONGO-2545 |
| 320 | + public static boolean isBatman() { |
| 321 | + return false; |
| 322 | + } |
| 323 | + |
273 | 324 | }
|
0 commit comments