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..c9698563 100644 --- a/src/SQLProvider.Common/Utils.fs +++ b/src/SQLProvider.Common/Utils.fs @@ -84,9 +84,42 @@ 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 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) = + match Type.GetType typename with + | null when not (String.IsNullOrEmpty typename) -> + 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. 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..358d14d3 --- /dev/null +++ b/tests/SqlProvider.Tests/SchemaTypeNameTests.fs @@ -0,0 +1,64 @@ +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" "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 "") 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