From be78ac811b0b302866e79eec8b289567e71f6e2b Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Thu, 19 Nov 2026 12:21:38 +0000 Subject: [PATCH 1/2] Improvement of saved schema datacontext type name resolution --- docs/RELEASE_NOTES.md | 3 ++ src/SQLProvider.Common/Utils.fs | 36 +++++++++++++-- .../SqlProvider.Tests/SchemaTypeNameTests.fs | 44 +++++++++++++++++++ .../SqlProvider.Tests.fsproj | 1 + 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 tests/SqlProvider.Tests/SchemaTypeNameTests.fs diff --git a/docs/RELEASE_NOTES.md b/docs/RELEASE_NOTES.md index 2572051b..8e40f4f1 100644 --- a/docs/RELEASE_NOTES.md +++ b/docs/RELEASE_NOTES.md @@ -1,3 +1,6 @@ +### 1.5.33 - 19.11.2026 +- Fix: a saved schema (ContextSchemaPath) loads on both .NET and .NET Framework, whichever saved it. Visual Studio, whose editor and compiler run on .NET Framework, failed on a schema saved by dotnet with "Value cannot be null. Parameter name: key" + ### 1.5.32 - 14.11.2026 - Fix: PostgreSQL functions are called inside a SELECT. With Npgsql 7 and newer they failed with "procedure ... does not exist", as a stored-procedure command is a CALL there - PostgreSQL procedures (11 and newer) are under ctx.Procedures and called with CALL, with any Npgsql version; they weren't listed before. INOUT and OUT parameters are the result's properties diff --git a/src/SQLProvider.Common/Utils.fs b/src/SQLProvider.Common/Utils.fs index e472b0eb..6d495175 100644 --- a/src/SQLProvider.Common/Utils.fs +++ b/src/SQLProvider.Common/Utils.fs @@ -84,9 +84,39 @@ module Utilities = parseAggregates' fieldNotation fieldNotationAlias tail parsed parseAggregates' fieldNotat fieldNotationAlias query [] - // https://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null - let getType typename = - Type.GetType typename + /// The type of an assembly-qualified or plain type name, or null. + /// A saved schema names types as the runtime that saved it had them: "System.String, System.Private.CoreLib, + /// Version=10.0.0.0, ..." from .NET, "System.String, mscorlib, Version=4.0.0.0, ..." from .NET Framework, and a type + /// such as System.Net.IPAddress lives in System.Net.Primitives on one and System on the other. So a name that doesn't + /// load as written is tried with its assembly's simple name only (any version), then by its full name alone (the + /// core library), then in the assemblies already loaded. Only the outermost assembly part is read: a generic type's + /// arguments are taken as written. + let getType (typename:string) = + // the comma outside any brackets: "System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib" + let assemblyComma () = + typename + |> Seq.indexed + |> Seq.scan (fun (depth, _) (i, c) -> + match c with + | '[' -> depth + 1, ValueNone + | ']' -> depth - 1, ValueNone + | ',' when depth = 0 -> depth, ValueSome i + | _ -> depth, ValueNone) (0, ValueNone) + |> Seq.tryPick (fun (_, i) -> match i with ValueSome i -> Some i | ValueNone -> None) + let elsewhere comma = + let fullName = typename.Substring(0, comma).Trim() + let assemblyName = typename.Substring(comma + 1).Split(',').[0].Trim() + seq { + yield fun () -> Type.GetType($"{fullName}, {assemblyName}", false) + yield fun () -> Type.GetType(fullName, false) + for a in AppDomain.CurrentDomain.GetAssemblies() do + if not a.IsDynamic then yield fun () -> a.GetType(fullName, false) + } + |> Seq.tryPick (fun attempt -> match (try attempt () with _ -> null) with null -> None | t -> Some t) + match Type.GetType typename with + | null when not (String.IsNullOrEmpty typename) -> + assemblyComma () |> Option.bind elsewhere |> Option.toObj + | t -> t /// Some/ValueSome case of the given option or voption type holding the value. let private makeSomeOf (optionType:Type) (value:obj) = diff --git a/tests/SqlProvider.Tests/SchemaTypeNameTests.fs b/tests/SqlProvider.Tests/SchemaTypeNameTests.fs new file mode 100644 index 00000000..d4d4736b --- /dev/null +++ b/tests/SqlProvider.Tests/SchemaTypeNameTests.fs @@ -0,0 +1,44 @@ +module SchemaTypeNameTests + +// A saved schema's type names come from the runtime that saved it; both runtimes must read both kinds. + +open System +open System.Net +open FSharp.Data.Sql.Common +open NUnit.Framework + +let private savedOnNet10 name assembly token = sprintf "%s, %s, Version=10.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token +let private savedOnNetFramework name assembly token = sprintf "%s, %s, Version=4.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token +let private coreLib = "7cec85d7bea7798e" +let private mscorlib = "b77a5c561934e089" + +[] +let ``a name saved on this runtime resolves`` () = + Assert.AreEqual(typeof, Utilities.getType typeof.AssemblyQualifiedName) + Assert.AreEqual(typeof, Utilities.getType "System.String") + +[] +let ``core library types saved on .NET resolve`` () = + Assert.AreEqual(typeof, Utilities.getType (savedOnNet10 "System.String" "System.Private.CoreLib" coreLib)) + Assert.AreEqual(typeof, Utilities.getType (savedOnNet10 "System.DateTime" "System.Private.CoreLib" coreLib)) + Assert.AreEqual(typeof, Utilities.getType (savedOnNet10 "System.Byte[]" "System.Private.CoreLib" coreLib)) + +[] +let ``core library types saved on .NET Framework resolve`` () = + Assert.AreEqual(typeof, Utilities.getType (savedOnNetFramework "System.Decimal" "mscorlib" mscorlib)) + Assert.AreEqual(typeof, Utilities.getType (savedOnNetFramework "System.Int32[]" "mscorlib" mscorlib)) + +[] +let ``a later version of the saving runtime still resolves`` () = + Assert.AreEqual(typeof, Utilities.getType "System.String, System.Private.CoreLib, Version=99.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e") + +[] +let ``a type in another assembly on the other runtime resolves`` () = + let ipAddress = typeof + Assert.AreEqual(ipAddress, Utilities.getType (savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "cc7b13ffcd2ddd51")) + Assert.AreEqual(ipAddress, Utilities.getType (savedOnNetFramework "System.Net.IPAddress" "System" mscorlib)) + +[] +let ``an unknown type is null`` () = + Assert.IsNull(Utilities.getType "No.Such.Type") + Assert.IsNull(Utilities.getType "No.Such.Type, NoSuchAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") diff --git a/tests/SqlProvider.Tests/SqlProvider.Tests.fsproj b/tests/SqlProvider.Tests/SqlProvider.Tests.fsproj index 14951dfc..d1f30559 100644 --- a/tests/SqlProvider.Tests/SqlProvider.Tests.fsproj +++ b/tests/SqlProvider.Tests/SqlProvider.Tests.fsproj @@ -42,6 +42,7 @@ + Always From b7eb34d771994c49375ecc7300bae169b53167aa Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Thu, 19 Nov 2026 13:20:43 +0000 Subject: [PATCH 2/2] the type fetching continued --- src/SQLProvider.Common/Utils.fs | 53 ++++++++++--------- .../SqlProvider.Tests/SchemaTypeNameTests.fs | 22 +++++++- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/SQLProvider.Common/Utils.fs b/src/SQLProvider.Common/Utils.fs index 6d495175..c9698563 100644 --- a/src/SQLProvider.Common/Utils.fs +++ b/src/SQLProvider.Common/Utils.fs @@ -88,34 +88,37 @@ module Utilities = /// A saved schema names types as the runtime that saved it had them: "System.String, System.Private.CoreLib, /// Version=10.0.0.0, ..." from .NET, "System.String, mscorlib, Version=4.0.0.0, ..." from .NET Framework, and a type /// such as System.Net.IPAddress lives in System.Net.Primitives on one and System on the other. So a name that doesn't - /// load as written is tried with its assembly's simple name only (any version), then by its full name alone (the - /// core library), then in the assemblies already loaded. Only the outermost assembly part is read: a generic type's - /// arguments are taken as written. + /// load as written is read again part by part, generic arguments included: an assembly is loaded as named, then by + /// its simple name (any version), and a type that isn't in its named assembly is looked for in the core library and + /// then in the assemblies already loaded, public types only. A part that names no assembly is not looked for there. let getType (typename:string) = - // the comma outside any brackets: "System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib" - let assemblyComma () = - typename - |> Seq.indexed - |> Seq.scan (fun (depth, _) (i, c) -> - match c with - | '[' -> depth + 1, ValueNone - | ']' -> depth - 1, ValueNone - | ',' when depth = 0 -> depth, ValueSome i - | _ -> depth, ValueNone) (0, ValueNone) - |> Seq.tryPick (fun (_, i) -> match i with ValueSome i -> Some i | ValueNone -> None) - let elsewhere comma = - let fullName = typename.Substring(0, comma).Trim() - let assemblyName = typename.Substring(comma + 1).Split(',').[0].Trim() - seq { - yield fun () -> Type.GetType($"{fullName}, {assemblyName}", false) - yield fun () -> Type.GetType(fullName, false) - for a in AppDomain.CurrentDomain.GetAssemblies() do - if not a.IsDynamic then yield fun () -> a.GetType(fullName, false) - } - |> Seq.tryPick (fun attempt -> match (try attempt () with _ -> null) with null -> None | t -> Some t) match Type.GetType typename with | null when not (String.IsNullOrEmpty typename) -> - assemblyComma () |> Option.bind elsewhere |> Option.toObj + let coreLibrary = typeof.Assembly + let load (name:Reflection.AssemblyName) = try Reflection.Assembly.Load name with _ -> null + // Never null: Type.GetType gives up on a null assembly without asking for the type. + let assemblyOf (name:Reflection.AssemblyName) = + match load name with + | null -> + match load (Reflection.AssemblyName name.Name) with + | null -> coreLibrary + | a -> a + | a -> a + let typeOf (assembly:Reflection.Assembly) (name:string) (ignoreCase:bool) = + let publicTypeIn (a:Reflection.Assembly) = + match (try a.GetType(name, false, ignoreCase) with _ -> null) with + | null -> None + | t -> if t.IsVisible then Some t else None + match assembly with + | null -> Type.GetType(name, false, ignoreCase) + | named -> + seq { yield named + yield coreLibrary + for a in AppDomain.CurrentDomain.GetAssemblies() do + if not a.IsDynamic then yield a } + |> Seq.tryPick publicTypeIn + |> Option.toObj + try Type.GetType(typename, Func<_,_> assemblyOf, Func<_,_,_,_> typeOf, false) with _ -> null | t -> t /// Some/ValueSome case of the given option or voption type holding the value. diff --git a/tests/SqlProvider.Tests/SchemaTypeNameTests.fs b/tests/SqlProvider.Tests/SchemaTypeNameTests.fs index d4d4736b..358d14d3 100644 --- a/tests/SqlProvider.Tests/SchemaTypeNameTests.fs +++ b/tests/SqlProvider.Tests/SchemaTypeNameTests.fs @@ -35,10 +35,30 @@ let ``a later version of the saving runtime still resolves`` () = [] let ``a type in another assembly on the other runtime resolves`` () = let ipAddress = typeof - Assert.AreEqual(ipAddress, Utilities.getType (savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "cc7b13ffcd2ddd51")) + Assert.AreEqual(ipAddress, Utilities.getType (savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a")) Assert.AreEqual(ipAddress, Utilities.getType (savedOnNetFramework "System.Net.IPAddress" "System" mscorlib)) +[] +let ``the arguments of a generic type resolve as well`` () = + let stringOnNet10 = savedOnNet10 "System.String" "System.Private.CoreLib" coreLib + let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNet10 stringOnNet10 + Assert.AreEqual(typeof>, Utilities.getType (savedOnNet10 hstore "System.Private.CoreLib" coreLib)) + let stringOnNetFramework = savedOnNetFramework "System.String" "mscorlib" mscorlib + let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNetFramework stringOnNetFramework + Assert.AreEqual(typeof>, Utilities.getType (savedOnNetFramework hstore "mscorlib" mscorlib)) + let nullableInts = sprintf "System.Nullable`1[[%s]][]" (savedOnNet10 "System.Int32" "System.Private.CoreLib" coreLib) + Assert.AreEqual(typeof[]>, Utilities.getType (savedOnNet10 nullableInts "System.Private.CoreLib" coreLib)) + let ipAddress = savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a" + Assert.AreEqual(typeof>, Utilities.getType (savedOnNet10 (sprintf "System.Lazy`1[[%s]]" ipAddress) "System.Private.CoreLib" coreLib)) + +[] +let ``a name without an assembly is not searched in the loaded assemblies`` () = + Assert.AreEqual(typeof, Utilities.getType "NUnit.Framework.Assert, No.Such.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") + Assert.IsNull(Utilities.getType "NUnit.Framework.Assert") + [] let ``an unknown type is null`` () = Assert.IsNull(Utilities.getType "No.Such.Type") Assert.IsNull(Utilities.getType "No.Such.Type, NoSuchAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") + Assert.IsNull(Utilities.getType (savedOnNet10 "System.NoSuchType" "System.Private.CoreLib" coreLib)) + Assert.IsNull(Utilities.getType "")