Skip to content

Commit 457bc9e

Browse files
committed
fix: guid with contains
fixes #35
1 parent 99ce539 commit 457bc9e

5 files changed

Lines changed: 46 additions & 9 deletions

File tree

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,27 @@ public async Task can_filter_by_guid()
407407
people.Count.Should().Be(1);
408408
people[0].Id.Should().Be(fakePersonOne.Id);
409409
}
410+
411+
[Fact]
412+
public async Task can_filter_by_guid_contains()
413+
{
414+
// Arrange
415+
var testingServiceScope = new TestingServiceScope();
416+
var fakePersonOne = new FakeTestingPersonBuilder().Build();
417+
var fakePersonTwo = new FakeTestingPersonBuilder().Build();
418+
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
419+
420+
var input = $"""{nameof(TestingPerson.Id)} @=* "{fakePersonOne.Id}" """;
421+
422+
// Act
423+
var queryablePeople = testingServiceScope.DbContext().People;
424+
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input);
425+
var people = await appliedQueryable.ToListAsync();
426+
427+
// Assert
428+
people.Count.Should().Be(1);
429+
people[0].Id.Should().Be(fakePersonOne.Id);
430+
}
410431

411432
[Fact]
412433
public async Task return_no_records_when_no_match()

QueryKit.UnitTests/CustomFilterPropertyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void can_have_custom_prop_name_for_multiple_props()
119119
config.Property<TestingPerson>(x => x.Id).HasQueryName("identifier");
120120
});
121121
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
122-
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id == Parse("{guidValue}")))""");
122+
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id.ToString() == "{guidValue}"))""");
123123
}
124124

125125
[Fact]
@@ -135,7 +135,7 @@ public void can_have_custom_prop_name_for_some_props()
135135
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
136136
});
137137
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
138-
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id == Parse("{guidValue}")))""");
138+
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (x.Id.ToString() == "{guidValue}"))""");
139139
}
140140

141141
[Fact]

QueryKit.UnitTests/FilterParserTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void complex_with_lots_of_types()
4242

4343
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
4444
filterExpression.ToString().Should()
45-
.Be(""""x => (((((((x.Title.ToLower().Contains("waffle & chicken".ToLower()) AndAlso (x.Age > 30)) OrElse (x.Id == Parse("aa648248-cb69-4217-ac95-d7484795afb2"))) OrElse (x.Title == "lamb")) OrElse (x.Title == null)) AndAlso ((x.Age < 18) OrElse ((x.BirthMonth == new Nullable`1(January)) AndAlso x.Title.StartsWith("ally")))) OrElse (x.Rating > 3.5)) OrElse ((x.SpecificDate == new Nullable`1(new DateTimeOffset(637922304030000000, 00:00:00))) AndAlso ((x.Date == new Nullable`1(new DateOnly(2022, 7, 1))) OrElse (x.Time == new Nullable`1(new TimeOnly(0, 0, 3, 0, 0))))))"""");
45+
.Be(""""x => (((((((x.Title.ToLower().Contains("waffle & chicken".ToLower()) AndAlso (x.Age > 30)) OrElse (x.Id.ToString() == "aa648248-cb69-4217-ac95-d7484795afb2")) OrElse (x.Title == "lamb")) OrElse (x.Title == null)) AndAlso ((x.Age < 18) OrElse ((x.BirthMonth == new Nullable`1(January)) AndAlso x.Title.StartsWith("ally")))) OrElse (x.Rating > 3.5)) OrElse ((x.SpecificDate == new Nullable`1(new DateTimeOffset(637922304030000000, 00:00:00))) AndAlso ((x.Date == new Nullable`1(new DateOnly(2022, 7, 1))) OrElse (x.Time == new Nullable`1(new TimeOnly(0, 0, 3, 0, 0))))))"""");
4646
}
4747

4848
[Fact]
@@ -76,7 +76,7 @@ public void can_handle_guid_with_double_quotes()
7676
var guid = Guid.NewGuid();
7777
var input = $"""Id == "{guid}" """;
7878
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
79-
filterExpression.ToString().Should().Be($"x => (x.Id == Parse(\"{guid}\"))");
79+
filterExpression.ToString().Should().Be($"x => (x.Id.ToString() == \"{guid}\")");
8080
}
8181

8282
[Fact]
@@ -85,7 +85,7 @@ public void can_handle_guid_without_double_quotes()
8585
var guid = Guid.NewGuid();
8686
var input = $"""Id == {guid} """;
8787
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
88-
filterExpression.ToString().Should().Be($"x => (x.Id == Parse(\"{guid}\"))");
88+
filterExpression.ToString().Should().Be($"x => (x.Id.ToString() == \"{guid}\")");
8989
}
9090

9191
[Fact]
@@ -217,7 +217,7 @@ public void simple_in_operator_for_guid()
217217
var input = """Id ^^ ["6d623e92-d2cf-4496-a2df-f49fa77328ee"]""";
218218
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
219219
filterExpression.ToString().Should()
220-
.Be(""""x => value(System.Collections.Generic.List`1[System.Guid]).Contains(x.Id)"""");
220+
.Be(""""x => value(System.Collections.Generic.List`1[System.String]).Contains(x.Id.ToString())"""");
221221
}
222222

223223
[Fact]

QueryKit/FilterParser.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
196196

197197
if (right.StartsWith("[") && right.EndsWith("]"))
198198
{
199+
targetType = targetType == typeof(Guid) || targetType == typeof(Guid?) ? typeof(string) : targetType;
199200
var values = right.Trim('[', ']').Split(',').Select(x => x.Trim()).ToList();
200201
var elementType = targetType.IsArray ? targetType.GetElementType() : targetType;
201202

@@ -300,8 +301,14 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
300301

301302
if (targetType == typeof(Guid))
302303
{
303-
var guidParseMethod = typeof(Guid).GetMethod("Parse", new[] { typeof(string) });
304-
return Expression.Call(guidParseMethod, Expression.Constant(right, typeof(string)));
304+
// if (comparisonOperator == ComparisonOperator.InOperator() ||
305+
// comparisonOperator == ComparisonOperator.NotInOperator())
306+
// {
307+
// var guidParseMethod = typeof(Guid).GetMethod("Parse", new[] { typeof(string) });
308+
// return Expression.Call(guidParseMethod, Expression.Constant(right, typeof(string)));
309+
// }
310+
311+
return Expression.Constant(right, typeof(string));
305312
}
306313

307314
var convertedValue = conversionFunction(right);
@@ -393,6 +400,13 @@ private static Parser<Expression> ComparisonExprParser<T>(ParameterExpression pa
393400
{
394401
return Expression.Equal(Expression.Constant(true), Expression.Constant(true));
395402
}
403+
404+
if (temp.leftExpr.Type == typeof(Guid) || temp.leftExpr.Type == typeof(Guid?))
405+
{
406+
var toStringMethod = typeof(Guid).GetMethod("ToString", Type.EmptyTypes);
407+
var leftExpr = Expression.Call(temp.leftExpr, toStringMethod!);
408+
return temp.op.GetExpression<T>(leftExpr, CreateRightExpr(temp.leftExpr, temp.right), config?.DbContextType);
409+
}
396410

397411
var rightExpr = CreateRightExpr(temp.leftExpr, temp.right);
398412
return temp.op.GetExpression<T>(temp.leftExpr, rightExpr, config?.DbContextType);

QueryKit/Operators/ComparisonOperator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ public InType(bool caseInsensitive = false, bool usesAll = false) : base("^^", 1
474474
public override string Operator() => CaseInsensitive ? $"{Name}{CaseSensitiveAppendix}" : Name;
475475
public override Expression GetExpression<T>(Expression left, Expression right, Type? dbContextType)
476476
{
477-
var leftType = left.Type;
477+
var leftType = left.Type == typeof(Guid) || left.Type == typeof(Guid?)
478+
? typeof(string)
479+
: left.Type;
478480

479481
if (right is NewArrayExpression newArrayExpression)
480482
{

0 commit comments

Comments
 (0)