Skip to content

Commit

Permalink
Bugfix: support method call in Any() on embedded objects array (#446)
Browse files Browse the repository at this point in the history
* 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 <steve.lorello@redis.com>
  • Loading branch information
3 people authored May 2, 2024
1 parent 34fb4ca commit e89de3f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
[Logo]: images/logo.svg
Expand Down
13 changes: 11 additions & 2 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,46 @@ await _substitute.Received().ExecuteAsync(
"100");
}

[Fact]
public async Task TestAnyQueryForArrayOfEmbeddedObjectsMethodCallExpression()
{
_substitute.ClearSubstitute();
_substitute.ExecuteAsync(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var collection = new RedisCollection<ObjectWithEmbeddedArrayOfObjects>(_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()
{
Expand Down

0 comments on commit e89de3f

Please sign in to comment.