Skip to content
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
8 changes: 2 additions & 6 deletions QueryKit.IntegrationTests/Tests/PropertyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace QueryKit.IntegrationTests.Tests;
public class PropertyResolverTests : TestBase
{
[Fact]
public async Task unknown_property_clause_under_or_is_true_by_default()
public async Task unknown_property_clause_under_or_is_true_when_replaced_with_true()
{
// Arrange
var testingServiceScope = new TestingServiceScope();
Expand All @@ -26,6 +26,7 @@ public async Task unknown_property_clause_under_or_is_true_by_default()
var input = $"""Title == "{title}" && (Nope == "x" || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.ReplaceWithTrue;
config.AllowUnknownProperties = true;
});

Expand Down Expand Up @@ -53,7 +54,6 @@ public async Task unknown_property_clause_under_or_does_not_return_every_row()
var input = $"""Title == "{title}" && (Nope == "x" || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand Down Expand Up @@ -82,7 +82,6 @@ public async Task prevented_property_clause_under_or_does_not_return_every_row()
var input = $"""Title == "{title}" && (Rating == 1 || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Rating).PreventFilter();
});

Expand Down Expand Up @@ -111,7 +110,6 @@ public async Task prevented_property_clause_by_its_query_name_under_or_does_not_
var input = $"""Title == "{title}" && (first == "Paul" || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.FirstName).HasQueryName("first").PreventFilter().PreventSort();
});

Expand Down Expand Up @@ -168,7 +166,6 @@ public async Task prevented_property_on_the_right_side_is_not_compared()
var input = $"""Title == "{title}" && (FirstName == LastName || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.LastName).PreventFilter();
});

Expand Down Expand Up @@ -429,7 +426,6 @@ public async Task unknown_property_in_arithmetic_removes_the_clause_when_unknown
var input = $"""Title == "{title}" && ((Nope + 1) > 3 || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand Down
10 changes: 5 additions & 5 deletions QueryKit.UnitTests/CustomFilterPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void can_have_custom_prop_excluded_from_filter()
config.Property<TestingPerson>(x => x.Id).PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToDisplayString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (True == True))""");
filterExpression.ToDisplayString().Should().Be($"""x => (x.Title == "{stringValue}")""");
}

[Fact]
Expand All @@ -159,7 +159,7 @@ public void can_have_custom_prop_excluded_from_filter_with_custom_propname()
config.Property<TestingPerson>(x => x.Id).HasQueryName("identifier").PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToDisplayString().Should().Be($"""x => ((x.Title == "{stringValue}") OrElse (True == True))""");
filterExpression.ToDisplayString().Should().Be($"""x => (x.Title == "{stringValue}")""");
}

[Fact]
Expand Down Expand Up @@ -243,7 +243,7 @@ public void can_have_derived_prop_work_with_collection_filters()
}

[Fact]
public void filter_prevented_props_always_have_true_equals_true_regardless_of_comparison()
public void filter_prevented_props_are_removed_regardless_of_comparison()
{
var faker = new Faker();
var filterOperator = faker.PickRandom(ComparisonOperator.List.Where(x => x != ComparisonOperator.EqualsOperator()).ToList());
Expand All @@ -255,7 +255,7 @@ public void filter_prevented_props_always_have_true_equals_true_regardless_of_co
config.Property<TestingPerson>(x => x.Id).PreventFilter();
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToDisplayString().Should().Be("x => (True == True)");
filterExpression.ToDisplayString().Should().Be("x => True");
}

[Fact]
Expand Down Expand Up @@ -284,6 +284,6 @@ public void can_handle_nonexistent_property()
config.AllowUnknownProperties = true;
});
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
filterExpression.ToDisplayString().Should().Be("x => (True == True)");
filterExpression.ToDisplayString().Should().Be("x => True");
}
}
27 changes: 6 additions & 21 deletions QueryKit.UnitTests/PropertyResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
public class PropertyResolverTests
{
[Fact]
public void unknown_property_clause_is_true_equals_true_by_default()
public void unknown_property_clause_is_true_equals_true_when_replaced_with_true()
{
var input = """Nope == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.ReplaceWithTrue;
config.AllowUnknownProperties = true;
});

Expand All @@ -24,12 +25,13 @@
}

[Fact]
public void prevented_property_clause_is_true_equals_true_by_default()
public void prevented_property_clause_is_true_equals_true_when_replaced_with_true()
{
var input = """FirstName == "Ann" || Title == "s" """;
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.ReplaceWithTrue;
config.Property<TestingPerson>(x => x.Title).PreventFilter();

Check warning on line 34 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 34 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
Expand All @@ -38,13 +40,14 @@
}

[Fact]
public void property_list_with_only_prevented_properties_is_true_by_default()
public void property_list_with_only_prevented_properties_is_true_when_replaced_with_true()
{
var input = """(Title, FirstName) == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.ReplaceWithTrue;
config.Property<TestingPerson>(x => x.Title).PreventFilter();

Check warning on line 49 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 49 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
config.Property<TestingPerson>(x => x.FirstName).PreventFilter();

Check warning on line 50 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 50 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
Expand All @@ -58,7 +61,6 @@
var input = """Nope == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand All @@ -73,7 +75,6 @@
var input = """Age > 100 && Nope == "x" """;
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand All @@ -88,8 +89,7 @@
var input = """Rating == 1 || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Rating).PreventFilter();

Check warning on line 92 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 92 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
Expand All @@ -103,7 +103,6 @@
var input = """Title == "a" && (Nope == "x" || Age > 100)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand All @@ -118,9 +117,8 @@
var input = """(Title, FirstName) == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).PreventFilter();

Check warning on line 120 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.

Check warning on line 120 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
config.Property<TestingPerson>(x => x.FirstName).PreventFilter();

Check warning on line 121 in QueryKit.UnitTests/PropertyResolverTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference return.
});

var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
Expand All @@ -134,7 +132,6 @@
var input = """(Age + 0) > 10 || Title == "a" """;
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Age).PreventFilter();
});

Expand All @@ -149,7 +146,6 @@
var input = """(Age + 0) > (Rating * 2)""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Rating).PreventFilter();
});

Expand Down Expand Up @@ -178,7 +174,6 @@
var input = """FirstName == Title || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).PreventFilter();
});

Expand All @@ -193,7 +188,6 @@
var input = """FirstName == title || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).PreventFilter();
});

Expand Down Expand Up @@ -222,7 +216,6 @@
var input = """title == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).PreventFilter();
});

Expand All @@ -237,7 +230,6 @@
var input = """title == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).HasQueryName("t").PreventFilter();
});

Expand Down Expand Up @@ -295,7 +287,6 @@
var input = """full == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.DerivedProperty<TestingPerson>(x => x.FirstName + " " + x.LastName).HasQueryName("full").PreventFilter();
});

Expand Down Expand Up @@ -324,7 +315,6 @@
var input = """adult == true || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.CustomOperation<TestingPerson>((x, op, value) => x.Age > 17).HasQueryName("adult").PreventFilter();
});

Expand Down Expand Up @@ -530,7 +520,6 @@
var input = """name == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).HasQueryName("name").PreventFilter().PreventSort();
});

Expand All @@ -545,7 +534,6 @@
var input = """Title == "x" || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<TestingPerson>(x => x.Title).HasQueryName("name").PreventFilter().PreventSort();
});

Expand Down Expand Up @@ -597,7 +585,6 @@
var input = """Title == Author.Name || Directions == "x" """;
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.Property<Recipe>(x => x.Author.Name).PreventFilter();
});

Expand All @@ -612,7 +599,6 @@
var input = """(Nope + 1) > 3 || Age > 100""";
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand All @@ -627,7 +613,6 @@
var input = """(Age + 0) > Nope || Title == "a" """;
var config = new QueryKitConfiguration(config =>
{
config.IgnoredClauseBehavior = IgnoredClauseBehavior.Remove;
config.AllowUnknownProperties = true;
});

Expand Down
4 changes: 2 additions & 2 deletions QueryKit/Configuration/IgnoredClauseBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace QueryKit.Configuration;
/// </summary>
public enum IgnoredClauseBehavior
{
/// <summary>Default. Replaces the clause with (true == true). Under an OR, the whole OR is then true.</summary>
/// <summary>Replaces the clause with (true == true), the same as v1.14.2. Under an OR, the whole OR is then true.</summary>
ReplaceWithTrue = 0,

/// <summary>Removes the clause. A logical operator with a removed side keeps only its other side.</summary>
/// <summary>Default. Removes the clause. A logical operator with a removed side keeps only its other side.</summary>
Remove = 1
}
5 changes: 3 additions & 2 deletions QueryKit/Configuration/QueryKitSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public class QueryKitSettings

/// <summary>
/// What the filter parser does with a clause on a prevented or unknown property. The default is
/// <see cref="Configuration.IgnoredClauseBehavior.ReplaceWithTrue"/>, the same as v1.14.2.
/// <see cref="Configuration.IgnoredClauseBehavior.Remove"/>. Use
/// <see cref="Configuration.IgnoredClauseBehavior.ReplaceWithTrue"/> for the v1.14.2 behavior.
/// </summary>
public IgnoredClauseBehavior IgnoredClauseBehavior { get; set; } = IgnoredClauseBehavior.ReplaceWithTrue;
public IgnoredClauseBehavior IgnoredClauseBehavior { get; set; } = IgnoredClauseBehavior.Remove;

public QueryKitPropertyMapping<TModel> Property<TModel>(Expression<Func<TModel, object>>? propertySelector)
{
Expand Down
6 changes: 4 additions & 2 deletions QueryKit/FilterParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,11 @@ private static Parser<Expression> OrExprParser<T>(ParameterExpression parameter,
);

private static bool RemovesIgnoredClauses(IQueryKitConfiguration? config)
=> config is QueryKitConfiguration { IgnoredClauseBehavior: IgnoredClauseBehavior.Remove };
=> ((config as QueryKitConfiguration)?.IgnoredClauseBehavior ?? IgnoredClauseBehavior.Remove)
== IgnoredClauseBehavior.Remove;

// A clause on a prevented or unknown property. By default it becomes true == true, the same as v1.14.2.
// A clause on a prevented or unknown property. By default the parser removes it. With ReplaceWithTrue
// it becomes true == true, the same as v1.14.2.
private static Expression IgnoredClause(IQueryKitConfiguration? config)
=> RemovesIgnoredClauses(config)
? RemovedClauseExpression.Instance
Expand Down
Loading