|
| 1 | +namespace FSharp.Data.GraphQL.Server.Middleware |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Buffers |
| 5 | +open System.Collections.Generic |
| 6 | +open System.Reflection |
| 7 | +open System.Text.Json |
| 8 | +open FSharp.Data.GraphQL |
| 9 | +open FSharp.Data.GraphQL.Extensions |
| 10 | + |
| 11 | +[<RequireQualifiedAccess>] |
| 12 | +module TypeCoercion = |
| 13 | + |
| 14 | + /// Case-insensitive instance property lookup. The middleware lowercases field names during |
| 15 | + /// parsing, so we must also ignore casing here. |
| 16 | + let propertyBindFlags = |
| 17 | + BindingFlags.Public |
| 18 | + ||| BindingFlags.Instance |
| 19 | + ||| BindingFlags.IgnoreCase |
| 20 | + |
| 21 | + // Cached type references |
| 22 | + let private stringType = typeof<string> |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// If <paramref name="t"/> is <c>voption</c>, <c>option</c>, or <c>Skippable</c>, returns the inner type; otherwise <see langword="ValueNone"/>. |
| 26 | + /// </summary> |
| 27 | + let tryUnwrapOption (t : Type) : Type voption = |
| 28 | + if t.IsGenericType then |
| 29 | + let fullName = t.GetGenericTypeDefinition().FullName |
| 30 | + if |
| 31 | + fullName.StartsWith ReflectionHelper.ValueOptionTypeName |
| 32 | + || fullName.StartsWith ReflectionHelper.OptionTypeName |
| 33 | + || fullName.StartsWith ReflectionHelper.SkippableTypeName |
| 34 | + then |
| 35 | + ValueSome (t.GetGenericArguments().[0]) |
| 36 | + else |
| 37 | + ValueNone |
| 38 | + else |
| 39 | + ValueNone |
| 40 | + |
| 41 | + let unwrapOption (t : Type) : Type = |
| 42 | + tryUnwrapOption t |> ValueOption.defaultValue t |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// If <paramref name="t"/> is a generic collection, returns the element type. Handles both concrete collections (where <c>IEnumerable</c> is an |
| 46 | + /// implemented interface) and properties typed directly as <c>IEnumerable<T></c>. |
| 47 | + /// </summary> |
| 48 | + let tryUnwrapEnumerableElement (t : Type) : Type voption = |
| 49 | + let isEnumerableInterface (i : Type) = |
| 50 | + i.IsGenericType |
| 51 | + && Type.(=) (i.GetGenericTypeDefinition (), typedefof<IEnumerable<_>>) |
| 52 | + if Type.(=) (t, stringType) then |
| 53 | + ValueNone |
| 54 | + elif t.IsArray then |
| 55 | + t.GetElementType () |> ValueOption.ofObj |
| 56 | + elif isEnumerableInterface t then |
| 57 | + ValueSome (t.GetGenericArguments()[0]) |
| 58 | + else |
| 59 | + t.GetInterfaces () |
| 60 | + |> Array.vtryFind isEnumerableInterface |
| 61 | + |> ValueOption.map (fun i -> i.GetGenericArguments()[0]) |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Suffixes the middleware's parser preserves on <c>FieldFilter.FieldName</c> for scalar operators (e.g. <c>meetingId_eq</c>, <c> |
| 65 | + /// validFrom_gte</c>). They must be stripped before resolving the actual CLR property. |
| 66 | + /// These correspond to the lowercase variants produced after Phase 2 parsing in <c>SchemaDefinitions.parseFieldCondition</c>. Longer suffixes are |
| 67 | + /// listed first to prevent shorter ones (e.g. <c>_gt</c>) from incorrectly matching longer ones (e.g. <c>_gte</c>). |
| 68 | + /// </summary> |
| 69 | + let operatorSuffixes = |
| 70 | + [| |
| 71 | + // String operators (case-insensitive variants) |
| 72 | + FilterSuffixConstants.CI.StartsWithSuffix |
| 73 | + FilterSuffixConstants.CI.EndsWithSuffix |
| 74 | + FilterSuffixConstants.CI.SWSuffix |
| 75 | + FilterSuffixConstants.CI.EWSuffix |
| 76 | + FilterSuffixConstants.CI.ContainsSuffix |
| 77 | + FilterSuffixConstants.CI.EqualsSuffix |
| 78 | + FilterSuffixConstants.CI.EQSuffix |
| 79 | + // String operators (case-sensitive variants) |
| 80 | + FilterSuffixConstants.CS.StartsWithSuffix |
| 81 | + FilterSuffixConstants.CS.EndsWithSuffix |
| 82 | + FilterSuffixConstants.CS.SWSuffix |
| 83 | + FilterSuffixConstants.CS.EWSuffix |
| 84 | + FilterSuffixConstants.CS.ContainsSuffix |
| 85 | + FilterSuffixConstants.CS.EqualsSuffix |
| 86 | + FilterSuffixConstants.CS.EQSuffix |
| 87 | + // Numeric/comparison operators (from root) |
| 88 | + FilterSuffixConstants.GreaterThanOrEqualSuffix |
| 89 | + FilterSuffixConstants.LessThanOrEqualSuffix |
| 90 | + FilterSuffixConstants.GreaterThanSuffix |
| 91 | + FilterSuffixConstants.LessThanSuffix |
| 92 | + FilterSuffixConstants.GTESuffix |
| 93 | + FilterSuffixConstants.LTESuffix |
| 94 | + FilterSuffixConstants.GTSuffix |
| 95 | + FilterSuffixConstants.LTSuffix |
| 96 | + FilterSuffixConstants.InSuffix |
| 97 | + |] |
| 98 | + |
| 99 | + let stripOperatorSuffix (fieldName : string) : string = |
| 100 | + operatorSuffixes |
| 101 | + |> Array.vtryFind (fun s -> fieldName.EndsWith (s, StringComparison.OrdinalIgnoreCase)) |
| 102 | + |> ValueOption.map (fun s -> fieldName.Substring (0, fieldName.Length - s.Length)) |
| 103 | + |> ValueOption.defaultValue fieldName |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Writes a boxed GraphQL scalar primitive as a JSON token directly into <paramref name="writer"/>. Strings become JSON strings; numbers and |
| 107 | + /// booleans become raw JSON tokens. Returns <c>true</c> if the value was written; <c>false</c> if the type is unsupported. |
| 108 | + /// </summary> |
| 109 | + let private writeJsonValue (value : obj) (writer : Utf8JsonWriter) : bool = |
| 110 | + match value with |
| 111 | + | :? string as s -> |
| 112 | + writer.WriteStringValue s |
| 113 | + true |
| 114 | + | :? bool as b -> |
| 115 | + writer.WriteBooleanValue b |
| 116 | + true |
| 117 | + | :? int64 as n -> |
| 118 | + writer.WriteNumberValue n |
| 119 | + true |
| 120 | + | :? int as n -> |
| 121 | + writer.WriteNumberValue n |
| 122 | + true |
| 123 | + | :? double as n -> |
| 124 | + writer.WriteNumberValue n |
| 125 | + true |
| 126 | + | :? float32 as n -> |
| 127 | + writer.WriteNumberValue n |
| 128 | + true |
| 129 | + | :? decimal as n -> |
| 130 | + writer.WriteNumberValue n |
| 131 | + true |
| 132 | + | _ -> |
| 133 | + false |
| 134 | + |
| 135 | + // Suppress nullness warnings for the obj / objnull mixture. |
| 136 | +#nowarn "3261" |
| 137 | + /// <summary> |
| 138 | + /// Tries to coerce a value into <paramref name="targetType"/> using STJ deserialization. Primitives are written directly as JSON bytes via |
| 139 | + /// <see cref="writeJsonValue"/> into an <see cref="ArrayBufferWriter{T}"/>, then deserialized from <c>ReadOnlySpan<byte></c>. |
| 140 | + /// Already-correct values pass through unchanged. No intermediate string or <see cref="JsonDocument"/> is allocated. |
| 141 | + /// </summary> |
| 142 | + let tryCoerceValue (jsonOptions : JsonSerializerOptions voption) (targetType : Type) (value : objnull) : obj voption = |
| 143 | + if isNull value then |
| 144 | + ValueNone |
| 145 | + elif targetType.IsInstanceOfType value then |
| 146 | + ValueSome value |
| 147 | + else |
| 148 | + let buffer = ArrayBufferWriter<byte> 64 |
| 149 | + use writer = new Utf8JsonWriter (buffer) |
| 150 | + if not (writeJsonValue value writer) then |
| 151 | + ValueNone |
| 152 | + else |
| 153 | + writer.Flush () |
| 154 | + try |
| 155 | + let opts = jsonOptions |> ValueOption.defaultValue JsonSerializerOptions.Default |
| 156 | + JsonSerializer.Deserialize (buffer.WrittenSpan, targetType, opts) |> ValueSome |
| 157 | + with _ -> |
| 158 | + ValueNone |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Coerces an entire <see cref="ObjectListFilter"/> tree recursively by resolving the entities's properties and converting filter values into the |
| 162 | + /// property's CLR type. |
| 163 | + /// </summary> |
| 164 | + let rec coerceFilter (jsonOptions : JsonSerializerOptions voption) (entityType : Type) (filter : ObjectListFilter) : ObjectListFilter = |
| 165 | + match filter with |
| 166 | + | And (l, r) -> And (coerceFilter jsonOptions entityType l, coerceFilter jsonOptions entityType r) |
| 167 | + | Or (l, r) -> Or (coerceFilter jsonOptions entityType l, coerceFilter jsonOptions entityType r) |
| 168 | + | Not f -> Not (coerceFilter jsonOptions entityType f) |
| 169 | + | OfTypes _ -> filter |
| 170 | + | Equals (ff, cmp) -> |
| 171 | + match entityType.GetProperty (stripOperatorSuffix ff.FieldName, propertyBindFlags) with |
| 172 | + | null -> filter |
| 173 | + | prop -> |
| 174 | + let unwrapped = unwrapOption prop.PropertyType |
| 175 | + match tryCoerceValue jsonOptions unwrapped (box ff.Value) with |
| 176 | + | ValueNone -> filter |
| 177 | + | ValueSome (:? IComparable as coerced) -> Equals ({ ff with Value = coerced }, cmp) |
| 178 | + | ValueSome _ -> filter |
| 179 | + | GreaterThan ff |
| 180 | + | GreaterThanOrEqual ff |
| 181 | + | LessThan ff |
| 182 | + | LessThanOrEqual ff as originalFilter -> |
| 183 | + match entityType.GetProperty (stripOperatorSuffix ff.FieldName, propertyBindFlags) with |
| 184 | + | null -> filter |
| 185 | + | prop -> |
| 186 | + let unwrapped = unwrapOption prop.PropertyType |
| 187 | + match tryCoerceValue jsonOptions unwrapped (box ff.Value) with |
| 188 | + | ValueNone -> filter |
| 189 | + | ValueSome (:? IComparable as coerced) -> |
| 190 | + let coercedField = { ff with Value = coerced } |
| 191 | + match originalFilter with |
| 192 | + | GreaterThan _ -> GreaterThan coercedField |
| 193 | + | GreaterThanOrEqual _ -> GreaterThanOrEqual coercedField |
| 194 | + | LessThan _ -> LessThan coercedField |
| 195 | + | LessThanOrEqual _ -> LessThanOrEqual coercedField |
| 196 | + | _ -> filter |
| 197 | + | ValueSome _ -> filter |
| 198 | + | In ff -> |
| 199 | + match entityType.GetProperty (stripOperatorSuffix ff.FieldName, propertyBindFlags) with |
| 200 | + | null -> filter |
| 201 | + | prop -> |
| 202 | + let unwrapped = unwrapOption prop.PropertyType |
| 203 | + |
| 204 | + let struct (coercedValues, failedValues) = |
| 205 | + ff.Value |
| 206 | + |> List.fold |
| 207 | + (fun struct (coerced, failed) value -> |
| 208 | + match tryCoerceValue jsonOptions unwrapped value with |
| 209 | + | ValueSome coercedValue -> (coercedValue :: coerced, failed) |
| 210 | + | ValueNone -> struct (coerced, value :: failed)) |
| 211 | + ([], []) |
| 212 | + |
| 213 | + match failedValues with |
| 214 | + | [] -> In { ff with Value = List.rev coercedValues } |
| 215 | + | _ -> |
| 216 | + let failedValuesText = |
| 217 | + failedValues |
| 218 | + |> Seq.rev |
| 219 | + |> Seq.map (sprintf "%A") |
| 220 | + |> String.concat ", " |
| 221 | + |
| 222 | + invalidArg |
| 223 | + (nameof filter) |
| 224 | + ($"Unable to coerce one or more values for '{ff.FieldName}' to '{unwrapped.FullName}'. Uncoerced values: [{failedValuesText}]") |
| 225 | + | StartsWith (ff, cmp) |
| 226 | + | EndsWith (ff, cmp) as originalFilter -> |
| 227 | + match entityType.GetProperty (stripOperatorSuffix ff.FieldName, propertyBindFlags) with |
| 228 | + | null -> filter |
| 229 | + | _ -> |
| 230 | + match tryCoerceValue jsonOptions stringType (box ff.Value) with |
| 231 | + | ValueNone -> filter |
| 232 | + | ValueSome coerced -> |
| 233 | + let coercedField = { ff with Value = coerced :?> string } |
| 234 | + match originalFilter with |
| 235 | + | StartsWith (_, cmp) -> StartsWith (coercedField, cmp) |
| 236 | + | EndsWith (_, cmp) -> EndsWith (coercedField, cmp) |
| 237 | + | _ -> filter |
| 238 | + | Contains (ff, cmp) -> |
| 239 | + match entityType.GetProperty (stripOperatorSuffix ff.FieldName, propertyBindFlags) with |
| 240 | + | null -> filter |
| 241 | + | prop -> |
| 242 | + let unwrapped = unwrapOption prop.PropertyType |
| 243 | + let coercionTarget = |
| 244 | + match tryUnwrapEnumerableElement unwrapped with |
| 245 | + | ValueSome elementType -> elementType |
| 246 | + | ValueNone -> stringType |
| 247 | + match tryCoerceValue jsonOptions coercionTarget (box ff.Value) with |
| 248 | + | ValueNone -> filter |
| 249 | + | ValueSome (:? IComparable as coerced) -> Contains ({ ff with Value = coerced }, cmp) |
| 250 | + | ValueSome _ -> filter |
| 251 | + | FilterField ff -> |
| 252 | + match entityType.GetProperty (ff.FieldName, propertyBindFlags) with |
| 253 | + | null -> filter |
| 254 | + | prop -> |
| 255 | + let unwrapped = unwrapOption prop.PropertyType |
| 256 | + let nestedType = |
| 257 | + tryUnwrapEnumerableElement unwrapped |
| 258 | + |> ValueOption.defaultValue unwrapped |
| 259 | + FilterField { FieldName = ff.FieldName; Value = coerceFilter jsonOptions nestedType ff.Value } |
0 commit comments