File tree Expand file tree Collapse file tree 2 files changed +16
-10
lines changed
src/FSharp.MongoDB.Bson/Serialization/Conventions
tests/FSharp.MongoDB.Bson.Tests/Serialization Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -43,9 +43,8 @@ type FSharpRecordConvention() =
43
43
// Map each field of the record type.
44
44
fields |> Array.iter ( fun pi ->
45
45
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
50
49
)
51
50
| _ -> ()
Original file line number Diff line number Diff line change @@ -19,14 +19,12 @@ open FsUnit
19
19
open NUnit.Framework
20
20
21
21
module FSharpNRTSerialization =
22
-
23
22
type Primitive =
24
23
{ String : string | null
25
24
Int: int }
26
25
27
- [<RequireQualifiedAccess>]
28
- type Primitive2 =
29
- { String : string
26
+ type PrimitiveNoNull =
27
+ { StringNotNull : string
30
28
Int: int }
31
29
32
30
@@ -43,9 +41,11 @@ module FSharpNRTSerialization =
43
41
44
42
[<Test>]
45
43
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 ) ])
47
47
48
- let result = deserialize< Primitive2 > doc
48
+ let result = deserialize< Primitive > doc
49
49
let expected = { String = null
50
50
Int = 42 }
51
51
@@ -72,3 +72,10 @@ module FSharpNRTSerialization =
72
72
Int = 42 }
73
73
74
74
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>
You can’t perform that action at this time.
0 commit comments