Skip to content

fixing issue with negated method calls #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,26 @@ internal static string GetOperandString(MethodCallExpression exp)
/// </summary>
/// <param name="exp">expression.</param>
/// <param name="treatEnumsAsInt">Treat enum as an integer.</param>
/// <param name="negate">Whether or not to negate the result.</param>
/// <returns>the operand string.</returns>
/// <exception cref="ArgumentException">thrown if expression is un-parseable.</exception>
internal static string GetOperandStringForQueryArgs(Expression exp, bool treatEnumsAsInt = false)
internal static string GetOperandStringForQueryArgs(Expression exp, bool treatEnumsAsInt = false, bool negate = false)
{
return exp switch
var res = exp switch
{
ConstantExpression constExp => $"{constExp.Value}",
MemberExpression member => GetOperandStringForMember(member, treatEnumsAsInt),
MethodCallExpression method => TranslateMethodStandardQuerySyntax(method),
UnaryExpression unary => GetOperandStringForQueryArgs(unary.Operand, treatEnumsAsInt),
UnaryExpression unary => GetOperandStringForQueryArgs(unary.Operand, treatEnumsAsInt, unary.NodeType == ExpressionType.Not),
_ => throw new ArgumentException("Unrecognized Expression type")
};

if (negate)
{
return $"-{res}";
}

return res;
}

/// <summary>
Expand Down
31 changes: 20 additions & 11 deletions src/Redis.OM/Common/ExpressionTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,25 +320,34 @@ internal static string TranslateBinaryExpression(BinaryExpression binExpression)
}
else if (binExpression.Left is UnaryExpression uni)
{
member = (MemberExpression)uni.Operand;
var attr = member.Member.GetCustomAttributes(typeof(JsonConverterAttribute)).FirstOrDefault() as JsonConverterAttribute;
if (attr != null && attr.ConverterType == typeof(JsonStringEnumConverter))
string predicate;
if (uni.NodeType != ExpressionType.Not)
{
if (int.TryParse(rightContent, out int ordinal))
member = (MemberExpression)uni.Operand;
var attr = member.Member.GetCustomAttributes(typeof(JsonConverterAttribute)).FirstOrDefault() as JsonConverterAttribute;
if (attr != null && attr.ConverterType == typeof(JsonStringEnumConverter))
{
rightContent = Enum.ToObject(member.Type, ordinal).ToString();
if (int.TryParse(rightContent, out int ordinal))
{
rightContent = Enum.ToObject(member.Type, ordinal).ToString();
}
}
else
{
if (!int.TryParse(rightContent, out _) && !long.TryParse(rightContent, out _))
{
var type = Nullable.GetUnderlyingType(member.Type) ?? member.Type;
rightContent = ((int)Enum.Parse(type, rightContent)).ToString();
}
}

predicate = BuildQueryPredicate(binExpression.NodeType, leftContent, rightContent, member);
}
else
{
if (!int.TryParse(rightContent, out _) && !long.TryParse(rightContent, out _))
{
var type = Nullable.GetUnderlyingType(member.Type) ?? member.Type;
rightContent = ((int)Enum.Parse(type, rightContent)).ToString();
}
predicate = $"{leftContent}{SplitPredicateSeporators(binExpression.NodeType)}{rightContent}";
}

var predicate = BuildQueryPredicate(binExpression.NodeType, leftContent, rightContent, member);
sb.Append("(");
sb.Append(predicate);
sb.Append(")");
Expand Down
31 changes: 31 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Redis.OM;
using Redis.OM.Contracts;
Expand Down Expand Up @@ -2975,6 +2976,36 @@ public void TestMixedNestingQuerying()
));
}

[Fact]
public void TestMultipleContains()
{
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(_mockReply);
var collection = new RedisCollection<ObjectWithMultipleSearchableFields>(_mock.Object);
Expression<Func<ObjectWithMultipleSearchableFields, bool>> whereExpressionFail = a => !a.FirstName.Contains("Andrey") && !a.LastName.Contains("Bred");

collection.Where(whereExpressionFail).ToList();
whereExpressionFail = a => !a.FirstName.Contains("Andrey") && a.LastName.Contains("Bred");
collection.Where(whereExpressionFail).ToList();
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithmultiplesearchablefields-idx",
"(-(@FirstName:Andrey) -(@LastName:Bred))",
"LIMIT",
"0",
"100"
));

_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithmultiplesearchablefields-idx",
"(-(@FirstName:Andrey) (@LastName:Bred))",
"LIMIT",
"0",
"100"
));

}

[Fact]
public void TestSelectNestedObject()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests;

[Document(StorageType = StorageType.Hash)]
public class ObjectWithMultipleSearchableFields
{
[RedisIdField]
[Indexed]
public string Id { get; set; }

[Searchable]
public string FirstName { get; set; }

[Searchable]
public string LastName { get; set; }
}