Skip to content

Commit 2f73d59

Browse files
authored
fix: collections filtering not working when custom properties set
Fix collections filtering not working when custom properties set
2 parents 66786c8 + 696d6ed commit 2f73d59

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

QueryKit.UnitTests/CustomFilterPropertyTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,38 @@ public void can_have_custom_prop_excluded_from_filter_with_custom_propname()
187187
filterExpression.ToString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (True == True))""");
188188
}
189189

190+
[Fact]
191+
public void can_have_custom_prop_work_with_collection_filters()
192+
{
193+
var faker = new Faker();
194+
var stringValue = faker.Lorem.Word();
195+
var input = $"""special_title == "{stringValue}" && Ingredients.Name == "flour" """;
196+
197+
var config = new QueryKitConfiguration(config =>
198+
{
199+
config.Property<Recipe>(x => x.Title).HasQueryName("special_title");
200+
});
201+
var filterExpression = FilterParser.ParseFilter<Recipe>(input, config);
202+
filterExpression.ToString().Should().Be(
203+
$"""x => ((x.Title == "{stringValue}") AndAlso x.Ingredients.Select(y => y.Name).Any(z => (z == "flour")))""");
204+
}
205+
206+
[Fact]
207+
public void can_have_derived_prop_work_with_collection_filters()
208+
{
209+
var faker = new Faker();
210+
var stringValue = faker.Lorem.Word();
211+
var input = $"""special_title_directions == "{stringValue}" && Ingredients.Name == "flour" """;
212+
213+
var config = new QueryKitConfiguration(config =>
214+
{
215+
config.DerivedProperty<Recipe>(x => x.Title + x.Directions).HasQueryName("special_title_directions");
216+
});
217+
var filterExpression = FilterParser.ParseFilter<Recipe>(input, config);
218+
filterExpression.ToString().Should().Be(
219+
$"""x => (((x.Title + x.Directions) == "{stringValue}") AndAlso x.Ingredients.Select(y => y.Name).Any(z => (z == "flour")))""");
220+
}
221+
190222
[Fact]
191223
public void filter_prevented_props_always_have_true_equals_true_regardless_of_comparison()
192224
{

QueryKit/ParameterReplacer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ public ParameterReplacer(ParameterExpression newParameter)
1313

1414
protected override Expression VisitParameter(ParameterExpression node)
1515
{
16-
// Replace all parameters with the new parameter
17-
return _newParameter;
16+
// Replace all parameters of the same type with the new parameter
17+
if (node.Type == _newParameter.Type)
18+
{
19+
return _newParameter;
20+
}
21+
22+
return base.VisitParameter(node);
1823
}
1924
}

0 commit comments

Comments
 (0)