Skip to content

Commit af9e981

Browse files
authored
Merge pull request #882 from Thorium/portable-schema-types
Portable schema types
2 parents be73b4a + b7eb34d commit af9e981

4 files changed

Lines changed: 104 additions & 3 deletions

File tree

‎docs/RELEASE_NOTES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 1.5.33 - 19.11.2026
2+
- 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"
3+
14
### 1.5.32 - 14.11.2026
25
- 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
36
- 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

‎src/SQLProvider.Common/Utils.fs‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,42 @@ module Utilities =
8484
parseAggregates' fieldNotation fieldNotationAlias tail parsed
8585
parseAggregates' fieldNotat fieldNotationAlias query []
8686

87-
// https://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null
88-
let getType typename =
89-
Type.GetType typename
87+
/// The type of an assembly-qualified or plain type name, or null.
88+
/// A saved schema names types as the runtime that saved it had them: "System.String, System.Private.CoreLib,
89+
/// Version=10.0.0.0, ..." from .NET, "System.String, mscorlib, Version=4.0.0.0, ..." from .NET Framework, and a type
90+
/// such as System.Net.IPAddress lives in System.Net.Primitives on one and System on the other. So a name that doesn't
91+
/// load as written is read again part by part, generic arguments included: an assembly is loaded as named, then by
92+
/// its simple name (any version), and a type that isn't in its named assembly is looked for in the core library and
93+
/// then in the assemblies already loaded, public types only. A part that names no assembly is not looked for there.
94+
let getType (typename:string) =
95+
match Type.GetType typename with
96+
| null when not (String.IsNullOrEmpty typename) ->
97+
let coreLibrary = typeof<obj>.Assembly
98+
let load (name:Reflection.AssemblyName) = try Reflection.Assembly.Load name with _ -> null
99+
// Never null: Type.GetType gives up on a null assembly without asking for the type.
100+
let assemblyOf (name:Reflection.AssemblyName) =
101+
match load name with
102+
| null ->
103+
match load (Reflection.AssemblyName name.Name) with
104+
| null -> coreLibrary
105+
| a -> a
106+
| a -> a
107+
let typeOf (assembly:Reflection.Assembly) (name:string) (ignoreCase:bool) =
108+
let publicTypeIn (a:Reflection.Assembly) =
109+
match (try a.GetType(name, false, ignoreCase) with _ -> null) with
110+
| null -> None
111+
| t -> if t.IsVisible then Some t else None
112+
match assembly with
113+
| null -> Type.GetType(name, false, ignoreCase)
114+
| named ->
115+
seq { yield named
116+
yield coreLibrary
117+
for a in AppDomain.CurrentDomain.GetAssemblies() do
118+
if not a.IsDynamic then yield a }
119+
|> Seq.tryPick publicTypeIn
120+
|> Option.toObj
121+
try Type.GetType(typename, Func<_,_> assemblyOf, Func<_,_,_,_> typeOf, false) with _ -> null
122+
| t -> t
90123

91124
/// Some/ValueSome case of the given option or voption type holding the value.
92125
let private makeSomeOf (optionType:Type) (value:obj) =
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module SchemaTypeNameTests
2+
3+
// A saved schema's type names come from the runtime that saved it; both runtimes must read both kinds.
4+
5+
open System
6+
open System.Net
7+
open FSharp.Data.Sql.Common
8+
open NUnit.Framework
9+
10+
let private savedOnNet10 name assembly token = sprintf "%s, %s, Version=10.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token
11+
let private savedOnNetFramework name assembly token = sprintf "%s, %s, Version=4.0.0.0, Culture=neutral, PublicKeyToken=%s" name assembly token
12+
let private coreLib = "7cec85d7bea7798e"
13+
let private mscorlib = "b77a5c561934e089"
14+
15+
[<Test; SqlCheck.NoSql>]
16+
let ``a name saved on this runtime resolves`` () =
17+
Assert.AreEqual(typeof<Guid>, Utilities.getType typeof<Guid>.AssemblyQualifiedName)
18+
Assert.AreEqual(typeof<string>, Utilities.getType "System.String")
19+
20+
[<Test; SqlCheck.NoSql>]
21+
let ``core library types saved on .NET resolve`` () =
22+
Assert.AreEqual(typeof<string>, Utilities.getType (savedOnNet10 "System.String" "System.Private.CoreLib" coreLib))
23+
Assert.AreEqual(typeof<DateTime>, Utilities.getType (savedOnNet10 "System.DateTime" "System.Private.CoreLib" coreLib))
24+
Assert.AreEqual(typeof<byte[]>, Utilities.getType (savedOnNet10 "System.Byte[]" "System.Private.CoreLib" coreLib))
25+
26+
[<Test; SqlCheck.NoSql>]
27+
let ``core library types saved on .NET Framework resolve`` () =
28+
Assert.AreEqual(typeof<decimal>, Utilities.getType (savedOnNetFramework "System.Decimal" "mscorlib" mscorlib))
29+
Assert.AreEqual(typeof<int[]>, Utilities.getType (savedOnNetFramework "System.Int32[]" "mscorlib" mscorlib))
30+
31+
[<Test; SqlCheck.NoSql>]
32+
let ``a later version of the saving runtime still resolves`` () =
33+
Assert.AreEqual(typeof<string>, Utilities.getType "System.String, System.Private.CoreLib, Version=99.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e")
34+
35+
[<Test; SqlCheck.NoSql>]
36+
let ``a type in another assembly on the other runtime resolves`` () =
37+
let ipAddress = typeof<IPAddress>
38+
Assert.AreEqual(ipAddress, Utilities.getType (savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a"))
39+
Assert.AreEqual(ipAddress, Utilities.getType (savedOnNetFramework "System.Net.IPAddress" "System" mscorlib))
40+
41+
[<Test; SqlCheck.NoSql>]
42+
let ``the arguments of a generic type resolve as well`` () =
43+
let stringOnNet10 = savedOnNet10 "System.String" "System.Private.CoreLib" coreLib
44+
let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNet10 stringOnNet10
45+
Assert.AreEqual(typeof<Collections.Generic.IDictionary<string,string>>, Utilities.getType (savedOnNet10 hstore "System.Private.CoreLib" coreLib))
46+
let stringOnNetFramework = savedOnNetFramework "System.String" "mscorlib" mscorlib
47+
let hstore = sprintf "System.Collections.Generic.IDictionary`2[[%s],[%s]]" stringOnNetFramework stringOnNetFramework
48+
Assert.AreEqual(typeof<Collections.Generic.IDictionary<string,string>>, Utilities.getType (savedOnNetFramework hstore "mscorlib" mscorlib))
49+
let nullableInts = sprintf "System.Nullable`1[[%s]][]" (savedOnNet10 "System.Int32" "System.Private.CoreLib" coreLib)
50+
Assert.AreEqual(typeof<Nullable<int>[]>, Utilities.getType (savedOnNet10 nullableInts "System.Private.CoreLib" coreLib))
51+
let ipAddress = savedOnNet10 "System.Net.IPAddress" "System.Net.Primitives" "b03f5f7f11d50a3a"
52+
Assert.AreEqual(typeof<Lazy<IPAddress>>, Utilities.getType (savedOnNet10 (sprintf "System.Lazy`1[[%s]]" ipAddress) "System.Private.CoreLib" coreLib))
53+
54+
[<Test; SqlCheck.NoSql>]
55+
let ``a name without an assembly is not searched in the loaded assemblies`` () =
56+
Assert.AreEqual(typeof<Assert>, Utilities.getType "NUnit.Framework.Assert, No.Such.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
57+
Assert.IsNull(Utilities.getType "NUnit.Framework.Assert")
58+
59+
[<Test; SqlCheck.NoSql>]
60+
let ``an unknown type is null`` () =
61+
Assert.IsNull(Utilities.getType "No.Such.Type")
62+
Assert.IsNull(Utilities.getType "No.Such.Type, NoSuchAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
63+
Assert.IsNull(Utilities.getType (savedOnNet10 "System.NoSuchType" "System.Private.CoreLib" coreLib))
64+
Assert.IsNull(Utilities.getType "")

‎tests/SqlProvider.Tests/SqlProvider.Tests.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Compile Include="SqlServerTests.fs" Condition="'$(TargetFramework)' == 'net48'" />
4343
<Compile Include="MsDataSqliteTransactions.fs" />
4444
<Compile Include="PostgreSqlSchemaTests.fs" />
45+
<Compile Include="SchemaTypeNameTests.fs" />
4546
<Content Include="app.config" />
4647
<None Include="x86\SQLite.Interop.dll" Condition="'$(TargetFramework)' == 'net48'">
4748
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

0 commit comments

Comments
 (0)