From 6f9f070a30615645cc18ffdcd57e5da5cc06c12f Mon Sep 17 00:00:00 2001 From: Ricardo Zambon Date: Wed, 16 Sep 2026 09:31:34 -0300 Subject: [PATCH] fix: read a filter value into a nullable property instead of throwing Filtering by any nullable property failed before a row was read: InvalidCastException: Invalid cast from 'System.String' to 'System.Nullable`1[[System.Int64, ...]]' GetFilterValue switches on Type.GetTypeCode(resultType), and that answers Object for Nullable rather than Int64, so every nullable type fell through to the default branch and Convert.ChangeType -- which cannot produce a Nullable at all. IsEnum is false for Nullable for the same reason, so nullable enums went the same way. Nullable.GetUnderlyingType unwraps it once, at the top, and the rest of the method reasons about the underlying type. Returning the underlying value boxed is what callers already expect: RunFilter converts the constant to the member's own type, which is how a non-nullable enum has always arrived as its Int32. Measured against 1.13.2 with the payload that reported it, long?, int?, bool?, decimal?, double?, DateTime? and nullable enums every one threw; all of them now return the value, from a JSON number and from a JSON string alike. The non-nullable cases and string are byte for byte what they were. Co-Authored-By: Claude Opus 5 --- .../SummaryParametersExtensions.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ZWebAPI/ExtensionMethods/SummaryParametersExtensions.cs b/ZWebAPI/ExtensionMethods/SummaryParametersExtensions.cs index 9636c0a..088e5b2 100644 --- a/ZWebAPI/ExtensionMethods/SummaryParametersExtensions.cs +++ b/ZWebAPI/ExtensionMethods/SummaryParametersExtensions.cs @@ -68,7 +68,20 @@ public static bool HasFilter(this ISummaryParameters parameters, string property return null; } - switch (Type.GetTypeCode(resultType)) + // Nullable is unwrapped before anything asks what the type is. Type.GetTypeCode + // answers Object for Nullable, not Int64, so a nullable column fell through to + // the default branch and Convert.ChangeType threw -- it cannot produce a Nullable: + // + // InvalidCastException: Invalid cast from 'System.String' to + // 'System.Nullable`1[[System.Int64, ...]]' + // + // Filtering by any nullable property therefore failed before a row was read. + // IsEnum is false for Nullable for the same reason, so nullable enums are + // covered by unwrapping too. Returning the underlying value boxed is what callers + // already expect: RunFilter converts the constant to the member's own type. + Type targetType = Nullable.GetUnderlyingType(resultType) ?? resultType; + + switch (Type.GetTypeCode(targetType)) { case TypeCode.Boolean: if (jsonValue.ValueKind == JsonValueKind.String && bool.TryParse(jsonValue.GetString(), out bool boolResult)) @@ -171,11 +184,11 @@ public static bool HasFilter(this ISummaryParameters parameters, string property return null; } - if (resultType.IsEnum) + if (targetType.IsEnum) { - return Enum.Parse(resultType, rawValue.ToString() ?? string.Empty); + return Enum.Parse(targetType, rawValue.ToString() ?? string.Empty); } - return Convert.ChangeType(rawValue, resultType); + return Convert.ChangeType(rawValue, targetType); } } return null;