Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 3f9fe80

Browse files
committed
fix issue with empty EntityFilter
1 parent d744475 commit 3f9fe80

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

src/MediatR.CommandQuery/Queries/EntityFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class EntityFilter
2222
[JsonPropertyName("filters")]
2323
public IList<EntityFilter>? Filters { get; set; }
2424

25+
public bool IsValid() => (Filters?.Any(f => f.IsValid()) == true) || (Name is not null);
2526

2627
public override int GetHashCode()
2728
{

src/MediatR.CommandQuery/Queries/LinqExpressionBuilder.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class LinqExpressionBuilder
2626
};
2727

2828
private readonly StringBuilder _expression = new();
29-
private readonly List<object?> _values = new();
29+
private readonly List<object?> _values = [];
3030

3131
/// <summary>
3232
/// Gets the expression parameters.
@@ -68,6 +68,7 @@ private void Visit(EntityFilter entityFilter)
6868
WriteExpression(entityFilter);
6969
}
7070

71+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "MA0051:Method is too long", Justification = "Expression Logic")]
7172
private void WriteExpression(EntityFilter entityFilter)
7273
{
7374
// name require for expression
@@ -137,10 +138,8 @@ private void WriteExpression(EntityFilter entityFilter)
137138
private bool WriteGroup(EntityFilter entityFilter)
138139
{
139140
// check for group start
140-
var filters = entityFilter.Filters;
141-
142-
var hasGroup = filters?.Count > 0;
143-
if (!hasGroup)
141+
var filters = entityFilter.Filters?.Where(f => f.IsValid());
142+
if (filters?.Any() != true)
144143
return false;
145144

146145
var logic = string.IsNullOrWhiteSpace(entityFilter.Logic)
@@ -149,9 +148,6 @@ private bool WriteGroup(EntityFilter entityFilter)
149148

150149
var wroteFirst = false;
151150

152-
if (filters == null)
153-
return false;
154-
155151
_expression.Append('(');
156152
foreach (var filter in filters)
157153
{

test/MediatR.CommandQuery.Tests/Queries/LinqExpressionBuilderTest.cs

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public void FilterLogicalOr()
2525
var entityFilter = new EntityFilter
2626
{
2727
Logic = "or",
28-
Filters = new List<EntityFilter>
29-
{
28+
Filters =
29+
[
3030
new EntityFilter{ Name = "Rank", Value = 7 },
3131
new EntityFilter{ Name = "Name", Value = "Apple" }
32-
}
32+
]
3333
};
3434

3535
var builder = new LinqExpressionBuilder();
@@ -48,11 +48,11 @@ public void FilterLogicalAnd()
4848
{
4949
var entityFilter = new EntityFilter
5050
{
51-
Filters = new List<EntityFilter>
52-
{
51+
Filters =
52+
[
5353
new EntityFilter{ Name = "Rank", Value = 7 },
5454
new EntityFilter{ Name = "Name", Value = "Blueberry" }
55-
}
55+
]
5656

5757
};
5858

@@ -67,24 +67,47 @@ public void FilterLogicalAnd()
6767
builder.Parameters[1].Should().Be("Blueberry");
6868
}
6969

70+
[Fact]
71+
public void FilterLogicalAndEmpty()
72+
{
73+
var entityFilter = new EntityFilter
74+
{
75+
Filters =
76+
[
77+
new EntityFilter(),
78+
new EntityFilter{ Name = "Name", Value = "Blueberry" }
79+
]
80+
81+
};
82+
83+
var builder = new LinqExpressionBuilder();
84+
builder.Build(entityFilter);
85+
86+
builder.Expression.Should().NotBeEmpty();
87+
builder.Expression.Should().Be("(Name == @0)");
88+
89+
builder.Parameters.Count.Should().Be(1);
90+
builder.Parameters[0].Should().Be("Blueberry");
91+
}
92+
7093
[Fact]
7194
public void FilterComplex()
7295
{
7396
var entityFilter = new EntityFilter
7497
{
75-
Filters = new List<EntityFilter>
76-
{
98+
Filters =
99+
[
77100
new EntityFilter{ Name = "Rank", Operator = ">", Value = 5 },
78101
new EntityFilter
79102
{
80103
Logic = "or",
81-
Filters = new List<EntityFilter>
82-
{
104+
Filters =
105+
[
83106
new EntityFilter{ Name = "Name", Value = "Strawberry" },
84107
new EntityFilter{ Name = "Name", Value = "Blueberry" }
85-
}
108+
]
86109
}
87-
}
110+
]
88111
};
89112

90113
var builder = new LinqExpressionBuilder();
@@ -99,6 +122,36 @@ public void FilterComplex()
99122
builder.Parameters[2].Should().Be("Blueberry");
100123
}
101124

125+
[Fact]
126+
public void FilterComplexEmpty()
127+
{
128+
var entityFilter = new EntityFilter
129+
{
130+
Filters =
131+
[
132+
new EntityFilter{ Name = "Rank", Operator = ">", Value = 5 },
133+
new EntityFilter
134+
{
135+
Logic = "or",
136+
Filters =
137+
[
138+
new EntityFilter(),
139+
new EntityFilter()
140+
]
141+
}
142+
]
143+
};
144+
145+
var builder = new LinqExpressionBuilder();
146+
builder.Build(entityFilter);
147+
148+
builder.Expression.Should().NotBeEmpty();
149+
builder.Expression.Should().Be("(Rank > @0)");
150+
151+
builder.Parameters.Count.Should().Be(1);
152+
builder.Parameters[0].Should().Be(5);
153+
}
154+
102155
[Fact]
103156
public void FilterContains()
104157
{

0 commit comments

Comments
 (0)