diff --git a/QueryKit.UnitTests/HasConversionTests.cs b/QueryKit.UnitTests/HasConversionTests.cs index 35716ef..e251e7f 100644 --- a/QueryKit.UnitTests/HasConversionTests.cs +++ b/QueryKit.UnitTests/HasConversionTests.cs @@ -453,6 +453,115 @@ public void guid_with_has_conversion_compares_a_guid_constant(string queryName) filter.ToDisplayString().Should().Be($"x => (x.Id == {KnownGuid})"); } + [Fact] + public void can_filter_struct_with_in_operator_and_has_conversion() + { + // Arrange + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Id).HasConversion(); + }); + + // Act + var result = WrappedIdRows().ApplyQueryKitFilter("""Id ^^ ["2","3"]""", config).ToList(); + + // Assert + result.Count.Should().Be(1); + result[0].Name.Should().Be("two"); + } + + [Fact] + public void can_filter_struct_with_not_in_operator_and_has_conversion() + { + // Arrange + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Id).HasConversion(); + }); + + // Act + var result = WrappedIdRows().ApplyQueryKitFilter("""Id !^^ ["2","3"]""", config).ToList(); + + // Assert + result.Count.Should().Be(1); + result[0].Name.Should().Be("one"); + } + + [Fact] + public void can_filter_struct_with_in_operator_query_name_and_has_conversion() + { + // Arrange + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Id).HasQueryName("wrappedid").HasConversion(); + }); + + // Act + var result = WrappedIdRows().ApplyQueryKitFilter("""wrappedid ^^ ["1","2"]""", config).ToList(); + + // Assert + result.Select(x => x.Name).Should().BeEquivalentTo("one", "two"); + } + + [Fact] + public void can_filter_nullable_struct_with_in_operator_and_has_conversion() + { + // Arrange + var rows = new List + { + new() { Id = new WrappedId(1) }, + new() { Id = new WrappedId(2) }, + new() { Id = null } + }; + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Id!).HasConversion(); + }); + + // Act + var result = rows.ApplyQueryKitFilter("""Id ^^ ["2","3"]""", config).ToList(); + + // Assert + result.Count.Should().Be(1); + result[0].Id.Should().Be(new WrappedId(2)); + } + + [Fact] + public void can_filter_reference_type_with_in_operator_and_has_conversion() + { + // Arrange + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Email!).HasConversion(); + }); + + // Act + var result = EmailRows().ApplyQueryKitFilter("""Email ^^ ["b@x.com","c@x.com"]""", config).ToList(); + + // Assert + result.Count.Should().Be(1); + result[0].Email!.Value.Should().Be("b@x.com"); + } + + [Fact] + public void can_filter_reference_type_with_in_operator_and_comma_in_quoted_item_and_has_conversion() + { + // Arrange + var rows = EmailRows(); + rows.Add(new EmailRow { Email = new EmailAddressRecord("a,b@x.com") }); + var config = new QueryKitConfiguration(config => + { + config.Property(x => x.Email!).HasConversion(); + }); + + // Act + var result = rows.ApplyQueryKitFilter("""Email ^^ ["a,b@x.com","c@x.com"]""", config).ToList(); + + // Assert + result.Count.Should().Be(1); + result[0].Email!.Value.Should().Be("a,b@x.com"); + } + private static List WrappedIdRows() => new() { new() { Id = new WrappedId(1), Name = "one" }, diff --git a/QueryKit/FilterParser.cs b/QueryKit/FilterParser.cs index 5f56321..3c1d6c4 100644 --- a/QueryKit/FilterParser.cs +++ b/QueryKit/FilterParser.cs @@ -626,6 +626,12 @@ private static Expression CreateRightExprForProperty(Expression leftExpr, string // by constructing it from the string value using a constructor that takes the target type if (propertyConfig.ConversionTargetType == typeof(string)) { + // An in/not-in list has to stay an array of constants, so each item is converted on its own. + if (IsArrayLiteral(right) && CreateStringConversionArrayExpr(leftExpr.Type, right) is { } arrayExpr) + { + return arrayExpr; + } + // A null literal compares against null instead of being passed to the constructor var underlyingType = Nullable.GetUnderlyingType(leftExpr.Type); if (right == "null" && (!leftExpr.Type.IsValueType || underlyingType != null)) @@ -650,6 +656,40 @@ private static Expression CreateRightExprForProperty(Expression leftExpr, string return CreateRightExprFromType(targetType, right, rightIsQuotedLiteral, op); } + private static bool IsArrayLiteral(string right) + => right.StartsWith("[") && right.EndsWith("]"); + + // Builds the right side of an in/not-in filter for a property with HasConversion(). The in operator + // reads an array of constants, so every item is constructed from its string with the (string) constructor. + private static Expression? CreateStringConversionArrayExpr(Type leftType, string right) + { + var underlyingType = Nullable.GetUnderlyingType(leftType); + var stringCtor = (underlyingType ?? leftType).GetConstructor(new[] { typeof(string) }); + if (stringCtor == null) + { + return null; + } + + var items = right.Length == 2 ? new List() : SplitListItems(right); + + var constants = items.Select(item => + { + if (item == "null") + { + return Expression.Constant(null, leftType); + } + + if (item.Length >= 2 && item.StartsWith("\"") && item.EndsWith("\"")) + { + item = item.Substring(1, item.Length - 2); + } + + return Expression.Constant(stringCtor.Invoke(new object[] { item }), leftType); + }).ToArray(); + + return Expression.NewArrayInit(leftType, constants); + } + // True when CreateRightExprFromType can read a value of the type. For other types it throws. private static bool CanCreateRightExprFromType(Type type) {