Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 43 forks source link

WithSkippableOptionFields fails roundtrip when option inside union #175

Open sWW26 opened 1 year ago

sWW26 commented 1 year ago

Noticed this issue when using ThothLike().WithSkippableOptionFields() however a minimal repro can be achieved with WithSkippableOptionFields() alone.

Repro:

open System.Text.Json
open System.Text.Json.Serialization

let opts = JsonFSharpOptions().WithSkippableOptionFields().ToJsonSerializerOptions()

type MyUnion =
    | Case1 of int option
    | Case2 of bool option

type MyRecord = { Field1: MyUnion }

let json = JsonSerializer.Serialize({ Field1 = Case1 None }, opts)
let value = JsonSerializer.Deserialize<MyRecord>(json, opts)

Throws exception:

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $ | LineNumber: 0 | BytePositionInLine: 4.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'Null' as a number.