Skip to content

Commit

Permalink
add missing for non-null reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pchalamet committed Jan 1, 2025
1 parent 22aecc9 commit 8d673d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ type FSharpRecordConvention() =
// Map each field of the record type.
fields |> Array.iter (fun pi ->
let memberMap = classMap.MapMember(pi)
memberMap.SetDefaultValue(fun () -> null) |> ignore
// let nrtInfo = nrtContext.Create(pi)
// if nrtInfo.WriteState = NullabilityState.Nullable then
// memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore
let nrtInfo = nrtContext.Create(pi)
if nrtInfo.WriteState = NullabilityState.Nullable then
memberMap.SetDefaultValue(null).SetIsRequired(false) |> ignore
)
| _ -> ()
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ open FsUnit
open NUnit.Framework

module FSharpNRTSerialization =

type Primitive =
{ String : string | null
Int: int }

[<RequireQualifiedAccess>]
type Primitive2 =
{ String : string
type PrimitiveNoNull =
{ StringNotNull : string
Int: int }


Expand All @@ -43,9 +41,11 @@ module FSharpNRTSerialization =

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

let result = deserialize<Primitive2> doc
let result = deserialize<Primitive> doc
let expected = { String = null
Int = 42 }

Expand All @@ -72,3 +72,10 @@ module FSharpNRTSerialization =
Int = 42 }

result |> should equal expected

[<Test>]
let ``test deserialize with missing non-null reference shall fail``() =
let doc = BsonDocument([ BsonElement("Int", BsonInt32 42) ])

(fun () -> deserialize<PrimitiveNoNull> doc |> ignore)
|> should throw typeof<BsonSerializationException>

0 comments on commit 8d673d8

Please sign in to comment.