diff --git a/QueryKit.IntegrationTests/Tests/FilterParsingRegressionTests.cs b/QueryKit.IntegrationTests/Tests/FilterParsingRegressionTests.cs index a87ce98..3108862 100644 --- a/QueryKit.IntegrationTests/Tests/FilterParsingRegressionTests.cs +++ b/QueryKit.IntegrationTests/Tests/FilterParsingRegressionTests.cs @@ -46,6 +46,34 @@ public async Task operator_alias_text_inside_quoted_value_is_kept() people[0].Id.Should().Be(fakePersonOne.Id); } + [Fact] + public async Task padded_raw_string_matches_a_value_that_starts_and_ends_with_a_quote() + { + // Arrange + var testingServiceScope = new TestingServiceScope(); + var unquotedTitle = Guid.NewGuid().ToString(); + var quotedTitle = $"\"{unquotedTitle}\""; + var fakePersonOne = new FakeTestingPersonBuilder() + .WithTitle(quotedTitle) + .Build(); + var fakePersonTwo = new FakeTestingPersonBuilder() + .WithTitle(unquotedTitle) + .Build(); + await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo); + + var input = $""""{nameof(TestingPerson.Title)} == """ {quotedTitle} """ """"; + + // Act + var queryablePeople = testingServiceScope.DbContext().People; + var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input); + var people = await appliedQueryable.ToListAsync(); + + // Assert + people.Count.Should().Be(1); + people[0].Id.Should().Be(fakePersonOne.Id); + people[0].Title.Should().Be(quotedTitle); + } + [Fact] public async Task decimal_value_uses_invariant_culture() { diff --git a/QueryKit.UnitTests/QuotedListValueTests.cs b/QueryKit.UnitTests/QuotedListValueTests.cs index 21c51dc..ac8b972 100644 --- a/QueryKit.UnitTests/QuotedListValueTests.cs +++ b/QueryKit.UnitTests/QuotedListValueTests.cs @@ -27,6 +27,7 @@ public void quoted_value_in_list_form_is_a_string(string input, string[] expecte [InlineData("""Title ^^ "[abc]" """, new[] { "abc" })] [InlineData("""Title !^^ "[abc]" """, new[] { "[a]", "[a, b]" })] [InlineData("""Title ^^* "[ABC]" """, new[] { "abc" })] + [InlineData(""""Title ^^ """["abc", "[a]"]""" """", new[] { "abc", "[a]" })] [InlineData("""Score ^^ "[1, 3]" """, new[] { "abc", "[a, b]" })] [InlineData("""Size ^^ "[Small]" """, new[] { "[a]" })] public void in_operator_reads_a_quoted_list_as_a_list(string input, string[] expected) diff --git a/QueryKit.UnitTests/RawStringPaddingTests.cs b/QueryKit.UnitTests/RawStringPaddingTests.cs new file mode 100644 index 0000000..020af91 --- /dev/null +++ b/QueryKit.UnitTests/RawStringPaddingTests.cs @@ -0,0 +1,46 @@ +namespace QueryKit.UnitTests; + +using FluentAssertions; + +public class RawStringPaddingTests +{ + [Theory] + [InlineData(""""Title == """ "hi" """ """", new[] { "\"hi\"" })] + [InlineData(""""Title _= """ "hi" now """ """", new[] { "\"hi\" now" })] + [InlineData(""""Title _-= """ say "hi" """ """", new[] { "say \"hi\"" })] + [InlineData(""""Title @= """ "hi" """ """", new[] { "\"hi\"", "\"hi\" now", "say \"hi\"" })] + [InlineData(""""Title != """ "hi" """ """", new[] { "hi", "\"hi\" now", "say \"hi\"", " x ", "x", " x", " ", "\tx\t", "\"a\"", "b" })] + [InlineData(""""Title == """ x """ """", new[] { "x" })] + [InlineData(""""Title == """ x """ """", new[] { " x " })] + [InlineData(""""Title == """ x""" """", new[] { " x" })] + [InlineData(""""Title == """ """ """", new[] { " " })] + [InlineData("Title == \"\"\"\tx\t\"\"\"", new[] { "\tx\t" })] + [InlineData("""""Title == """"hi"""" """"", new[] { "hi" })] + [InlineData(""""Title ^^ [""" "a" """, "b"]"""", new[] { "\"a\"", "b" })] + public void raw_string_drops_one_space_of_padding_from_each_end(string input, string[] expected) + { + var result = Rows().AsQueryable().ApplyQueryKitFilter(input).ToList(); + + result.Select(x => x.Title).Should().BeEquivalentTo(expected); + } + + private static Row[] Rows() => + [ + new("\"hi\""), + new("hi"), + new("\"hi\" now"), + new("say \"hi\""), + new(" x "), + new("x"), + new(" x"), + new(" "), + new("\tx\t"), + new("\"a\""), + new("b"), + ]; + + public class Row(string title) + { + public string Title { get; } = title; + } +} diff --git a/QueryKit/FilterParser.cs b/QueryKit/FilterParser.cs index 2aca368..5f56321 100644 --- a/QueryKit/FilterParser.cs +++ b/QueryKit/FilterParser.cs @@ -368,7 +368,12 @@ from openingQuotes in Parse.Regex("\"{3,}").Text() let count = openingQuotes.Length from content in Parse.AnyChar.Except(Parse.Char('"').Repeat(count)).Many().Text() from closingQuotes in Parse.Char('"').Repeat(count).Text() - select content; + select RemoveRawStringPadding(content); + + private static string RemoveRawStringPadding(string content) + => content.StartsWith(' ') && content.EndsWith(' ') && content.Trim(' ').Length > 0 + ? content[1..^1] + : content; // Carries whether the right-hand value was written as a quoted string literal (e.g. "id"). // This is needed to disambiguate a literal from a bare property reference (property-to-property @@ -696,7 +701,7 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ var expressions = values.Select(x => { - if (elementType == typeof(string) && x.StartsWith("\"") && x.EndsWith("\"")) + if (rightIsQuotedLiteral && elementType == typeof(string) && x.StartsWith("\"") && x.EndsWith("\"")) { x = x.Trim('"'); } @@ -709,11 +714,6 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ return newArrayExpression; } - if (targetType == typeof(string)) - { - right = right.Trim('"'); - } - if (targetType == typeof(DateTime)) { var dtStyle = right.EndsWith("Z") ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.AssumeLocal; diff --git a/README.md b/README.md index 78ca623..60cd467 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,25 @@ Property list grouping is particularly useful for: var input = """""""""Title == """"lamb is great on a "gee-ro" not a "gy-ro" sandwich"""" """""""""; ``` + * The full run of quotes at the start of the value is the opening delimiter. The closing delimiter has the same number of quotes. Thus a quote at the start of the value becomes part of the delimiter. For example, `""""hi""""` is the value `hi` with a delimiter of four quotes. It is not the value `"hi"`. + + * To write a value that starts or ends with a double quote, put one space between each delimiter and the value. If the value starts with a space, ends with a space, and has a character that is not a space, QueryKit removes one space from each end. Only the space character counts. QueryKit does not remove tabs. The rule applies to every operator and to the items of a list, for example `Title ^^ [""" "a" """, "b"]`. In a list, QueryKit also removes all whitespace from the start and end of each item. Thus the table below is true only for a single value. + + | Value in the filter | Value that QueryKit uses | + | ------------------- | ------------------------ | + | `""" "hi" """` | `"hi"` | + | `""" "hi" now """` | `"hi" now` | + | `""" say "hi" """` | `say "hi"` | + | `""" x """` | `x` with one space on each side | + | `""" x"""` | `x` with one space before it. Only one end has a space, so QueryKit removes no space. | + | `""" """` | Three spaces. QueryKit does not change a value of only spaces. | + + ```c# + var input = """"Title == """ "hi" """ """"; + ``` + + * Before version 1.17.1, QueryKit kept the spaces. `""" x """` was `x` with one space on each side. Now it is `x`. Also, a raw string list item lost its edge quotes. `[""" "a" """]` was the item `a`. Now it is `"a"`. + * Dates and times use ISO 8601 format and should be surrounded by double quotes: * `DateOnly`: `var filterInput = """Birthday == "2022-07-01" """;` @@ -782,7 +801,7 @@ This is particularly useful when: - Database indexes are defined on uppercase column values (e.g., `CREATE INDEX ON people (UPPER(email))`) - You need SARGable queries for better performance -**Per-property override** — you can also control the mode per property, overriding the global setting: +**Per-property override.** You can also set the mode for one property. This setting overrides the global setting: ```csharp var config = new QueryKitConfiguration(settings =>