Skip to content
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

Add parenthesis on filter #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,11 @@ protected override string VisitLambdaExpression(LambdaExpression topExpression,

private bool HasParenthesis(ExpressionType expressionType)
{
var hasParenthesis = _expressionType.HasValue && expressionType switch

var hasParenthesis = expressionType switch
{
ExpressionType.And => true,
ExpressionType.AndAlso => true,
ExpressionType.And => true && _expressionType.HasValue,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a unit test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfreidahl Mind adding the unit test? Would love to see this PR get accepted.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZEXSM a unit test has been added

ExpressionType.AndAlso => true && _expressionType.HasValue,
ExpressionType.Or => true,
ExpressionType.OrElse => true,
_ => false,
Expand Down
23 changes: 23 additions & 0 deletions test/OData.QueryBuilder.Test/ODataQueryCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,29 @@ public void ODataQueryBuilderList_Filter_support_parentheses_Success()
$" and ODataKind/ODataCode/Code in ('123','512')");
}

[Fact(DisplayName = "Multiple filters wrap or in parenthesis => Success")]
public void ODataQueryBuilderList_Multiple_filters_wrap_or_in_parenthesis_Success()
{
var constStrIds = new[] { "123", "512" };
var constValue = 3;

var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter((s, f, o) => s.IdRule == constValue)
.Filter((s, f, o) => s.IsActive)
.Filter((s, f, o) => (f.Date(s.EndDate.Value) == default(DateTimeOffset?) || s.EndDate > DateTime.Today), useParenthesis: true)
.Filter((s, f, o) => (f.Date((DateTimeOffset)s.BeginDate) != default(DateTime?) || f.Date((DateTime)s.BeginDate) <= DateTime.Now), useParenthesis: true)
.Filter((s, f, o) => (o.In(s.ODataKind.ODataCode.Code, constStrIds)))
.ToUri();

uri.Should().Be($"http://mock/odata/ODataType?$filter=IdRule eq 3" +
$" and IsActive" +
$" and (date(EndDate) eq null or EndDate gt {DateTime.Today:s}Z)" +
$" and (date(BeginDate) ne null or date(BeginDate) le {DateTime.Now:s}Z)" +
$" and ODataKind/ODataCode/Code in ('123','512')");
}

[Theory(DisplayName = "Count value => Success")]
[InlineData(true)]
[InlineData(false)]
Expand Down