diff --git a/src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj b/src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj index 16bfccf..6a88462 100644 --- a/src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj +++ b/src/FSharp.MongoDB.Bson/FSharp.MongoDB.Bson.fsproj @@ -4,6 +4,7 @@ netstandard2.1 true enable + true diff --git a/src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs b/src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs index 6d8a4d0..6611087 100644 --- a/src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs +++ b/src/FSharp.MongoDB.Bson/Serialization/FSharpTypeHelpers.fs @@ -34,17 +34,21 @@ module private Helpers = /// /// Returns Some typ when typ is a record type, and None otherwise. /// - let (|IsRecord|_|) typ = - let isRecord typ = typ <> null && FSharpType.IsRecord(typ, bindingFlags) - whenType isRecord typ + let (|IsRecord|_|) = function + | Null -> None + | NonNull typ -> + let isRecord typ = FSharpType.IsRecord(typ, bindingFlags) + whenType isRecord typ /// /// Returns Some typ when typ is a top-level union type or when it represents a /// particular union case, and None otherwise. /// - let (|IsUnion|_|) (typ:System.Type) = - let isUnion typ = typ <> null && FSharpType.IsUnion(typ, bindingFlags) - whenType isUnion typ + let (|IsUnion|_|) = function + | Null -> None + | NonNull typ -> + let isUnion typ = FSharpType.IsUnion(typ, bindingFlags) + whenType isUnion typ /// /// Returns true if typ is a generic type with defintion 'GenericType. diff --git a/src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs b/src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs index 2aca1b5..a3e01ae 100644 --- a/src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs +++ b/src/FSharp.MongoDB.Bson/Serialization/FSharpValueSerializer.fs @@ -38,9 +38,10 @@ module FSharpValueSerializer = interface IBsonSerializationProvider with member __.GetSerializer typ = - let mkSerializer = function - | Some (typ:System.Type) -> System.Activator.CreateInstance typ :?> IBsonSerializer - | None -> null + let mkSerializer typ = + typ + |> Option.map (fun typ -> System.Activator.CreateInstance typ :?> IBsonSerializer) + |> Option.toObj match typ with | IsList typ -> Some (mkGenericUsingDef> typ) diff --git a/tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj b/tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj index 2924ecd..aeca538 100644 --- a/tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj +++ b/tests/FSharp.MongoDB.Bson.Tests/FSharp.MongoDB.Bson.Tests.fsproj @@ -2,8 +2,8 @@ net9.0 - false enable + true diff --git a/tests/FSharp.MongoDB.Bson.Tests/FSharpListSerializationTests.fs b/tests/FSharp.MongoDB.Bson.Tests/FSharpListSerializationTests.fs index 0b0aa9e..aa4bca2 100644 --- a/tests/FSharp.MongoDB.Bson.Tests/FSharpListSerializationTests.fs +++ b/tests/FSharp.MongoDB.Bson.Tests/FSharpListSerializationTests.fs @@ -87,13 +87,17 @@ module FSharpListSerialization = let value = { MaybeStrings = [ Some "a"; None; Some "z" ] } let result = serialize value - let expected = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ]) + let expected = + let values: (string|null) array = [| "a"; null; "z" |] + BsonDocument("MaybeStrings", BsonArray values) result |> should equal expected [] let ``test deserialize a list of optional strings``() = - let doc = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ]) + let doc = + let values: (string | null) array = [| "a"; null; "z" |] + BsonDocument("MaybeStrings", BsonArray values) let result = deserialize doc typeof let expected = { MaybeStrings = [ Some "a"; None; Some "z" ] } diff --git a/tests/FSharp.MongoDB.Bson.Tests/FSharpSetSerializationTests.fs b/tests/FSharp.MongoDB.Bson.Tests/FSharpSetSerializationTests.fs index 46f1380..63aa4d4 100644 --- a/tests/FSharp.MongoDB.Bson.Tests/FSharpSetSerializationTests.fs +++ b/tests/FSharp.MongoDB.Bson.Tests/FSharpSetSerializationTests.fs @@ -87,13 +87,17 @@ module FSharpSetSerialization = let value = { MaybeStrings = Set.ofList [ Some "a"; None; Some "z" ] } let result = serialize value - let expected = BsonDocument("MaybeStrings", BsonArray (Set.ofList ["a"; null; "z"])) + let expected = + let values: (string|null) list = [ "a"; null; "z" ] + BsonDocument("MaybeStrings", BsonArray (Set.ofList values)) result |> should equal expected [] let ``test deserialize a set of optional strings``() = - let doc = BsonDocument("MaybeStrings", BsonArray [ "a"; null; "z" ]) + let doc = + let values: (string|null) list = [ "a"; null; "z" ] + BsonDocument("MaybeStrings", BsonArray values) let result = deserialize doc typeof let expected = { MaybeStrings = Set.ofList [ Some "a"; None; Some "z" ] }