From e89de3f19e08a303c1321049849b9ee58d0164be Mon Sep 17 00:00:00 2001 From: Prudius Vladyslav <118296481+PrudiusVladislav@users.noreply.github.com> Date: Thu, 2 May 2024 17:05:54 +0300 Subject: [PATCH] Bugfix: support method call in Any() on embedded objects array (#446) * Bugfix: support method call in Any() on embedded list * adding StartsWith and EndsWith case --------- Co-authored-by: Steve Lorello <42971704+slorello89@users.noreply.github.com> Co-authored-by: slorello89 --- README.md | 1 + .../Common/ExpressionParserUtilities.cs | 13 +++++- .../RediSearchTests/SearchTests.cs | 40 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc903047..f2cd25a7 100644 --- a/README.md +++ b/README.md @@ -471,6 +471,7 @@ We'd love your contributions! If you want to contribute please read our [Contrib * [@jrpavoncello](https://github.com/jrpavoncello) * [@axnetg](https://github.com/axnetg) * [@abbottdev](https://github.com/abbottdev) +* [@PrudiusVladislav](https://github.com/PrudiusVladislav) [Logo]: images/logo.svg diff --git a/src/Redis.OM/Common/ExpressionParserUtilities.cs b/src/Redis.OM/Common/ExpressionParserUtilities.cs index 6ac85cd8..783f75db 100644 --- a/src/Redis.OM/Common/ExpressionParserUtilities.cs +++ b/src/Redis.OM/Common/ExpressionParserUtilities.cs @@ -960,8 +960,17 @@ private static string TranslateAnyForEmbeddedObjects(MethodCallExpression exp, L var type = exp.Arguments.Last().Type; var prefix = GetOperandString(exp.Arguments[0]); var lambda = (LambdaExpression)exp.Arguments.Last(); - var tempQuery = ExpressionTranslator.TranslateBinaryExpression((BinaryExpression)lambda.Body, parameters); - return tempQuery.Replace("@", $"{prefix}_"); + + if (lambda.Body is MethodCallExpression methodCall) + { + var tempQuery = TranslateMethodExpressions(methodCall, parameters); + return tempQuery.Replace("@", $"{prefix}_"); + } + else + { + var tempQuery = ExpressionTranslator.TranslateBinaryExpression((BinaryExpression)lambda.Body, parameters); + return tempQuery.Replace("@", $"{prefix}_"); + } } private static string ValueToString(object value) diff --git a/test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs b/test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs index f54eb564..9d2bce21 100644 --- a/test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs +++ b/test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs @@ -2237,6 +2237,46 @@ await _substitute.Received().ExecuteAsync( "100"); } + [Fact] + public async Task TestAnyQueryForArrayOfEmbeddedObjectsMethodCallExpression() + { + _substitute.ClearSubstitute(); + _substitute.ExecuteAsync(Arg.Any(), Arg.Any()).Returns(_mockReply); + + var collection = new RedisCollection(_substitute); + + await collection.Where(x => + x.Addresses.Any(a => a.City.Contains("Beach"))).ToListAsync(); + await collection.Where(x => + x.Addresses.Any(a => a.City.StartsWith("Satellite"))).ToListAsync(); + await collection.Where(x => + x.Addresses.Any(a => a.City.EndsWith("Beach"))).ToListAsync(); + + await _substitute.Received().ExecuteAsync( + "FT.SEARCH", + "objectwithembeddedarrayofobjects-idx", + "(@Addresses_City:{*Beach*})", + "LIMIT", + "0", + "100"); + + await _substitute.Received().ExecuteAsync( + "FT.SEARCH", + "objectwithembeddedarrayofobjects-idx", + "(@Addresses_City:{Satellite*})", + "LIMIT", + "0", + "100"); + + await _substitute.Received().ExecuteAsync( + "FT.SEARCH", + "objectwithembeddedarrayofobjects-idx", + "(@Addresses_City:{*Beach})", + "LIMIT", + "0", + "100"); + } + [Fact] public async Task SearchWithMultipleWhereClauses() {