Skip to content

Commit 8d673d8

Browse files
committed
add missing for non-null reference
1 parent 22aecc9 commit 8d673d8

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/FSharp.MongoDB.Bson/Serialization/Conventions/FSharpRecordConvention.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ type FSharpRecordConvention() =
4343
// Map each field of the record type.
4444
fields |> Array.iter (fun pi ->
4545
let memberMap = classMap.MapMember(pi)
46-
memberMap.SetDefaultValue(fun () -> null) |> ignore
47-
// let nrtInfo = nrtContext.Create(pi)
48-
// if nrtInfo.WriteState = NullabilityState.Nullable then
49-
// memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore
46+
let nrtInfo = nrtContext.Create(pi)
47+
if nrtInfo.WriteState = NullabilityState.Nullable then
48+
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore
5049
)
5150
| _ -> ()

tests/FSharp.MongoDB.Bson.Tests/Serialization/FSharpNRTSerializationTests.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ open FsUnit
1919
open NUnit.Framework
2020

2121
module FSharpNRTSerialization =
22-
2322
type Primitive =
2423
{ String : string | null
2524
Int: int }
2625

27-
[<RequireQualifiedAccess>]
28-
type Primitive2 =
29-
{ String : string
26+
type PrimitiveNoNull =
27+
{ StringNotNull : string
3028
Int: int }
3129

3230

@@ -43,9 +41,11 @@ module FSharpNRTSerialization =
4341

4442
[<Test>]
4543
let ``test deserialize nullable reference (null) in a record type)``() =
46-
let doc = BsonDocument([ BsonElement("Int", BsonInt32 42) ])
44+
// FIXME: once .net 9.0.200 is released, String can be omitted
45+
let doc = BsonDocument([ BsonElement("String", BsonNull.Value)
46+
BsonElement("Int", BsonInt32 42) ])
4747

48-
let result = deserialize<Primitive2> doc
48+
let result = deserialize<Primitive> doc
4949
let expected = { String = null
5050
Int = 42 }
5151

@@ -72,3 +72,10 @@ module FSharpNRTSerialization =
7272
Int = 42 }
7373

7474
result |> should equal expected
75+
76+
[<Test>]
77+
let ``test deserialize with missing non-null reference shall fail``() =
78+
let doc = BsonDocument([ BsonElement("Int", BsonInt32 42) ])
79+
80+
(fun () -> deserialize<PrimitiveNoNull> doc |> ignore)
81+
|> should throw typeof<BsonSerializationException>

0 commit comments

Comments
 (0)