Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
323 stars 44 forks source link

[Bug] using `DefaultIgnoreCondition = WhenWritingNull` deserializes missing fields as null #140

Closed nordfjord closed 1 year ago

nordfjord commented 1 year ago

I think this is related to #123

#r "nuget: FSharp.SystemTextJson, *"

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

let opts = JsonSerializerOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)
opts.Converters.Add(JsonFSharpConverter())

let b = JsonSerializer.Deserialize<{| name: string |}>("{}", opts)

printfn "%A" b

As it stands this .fsx will print { name = null } which is wholly unexpected from my perspective.

I would expect it to throw and complain about a missing field.

Did I configure it incorrectly?

samritchie commented 1 year ago

I am really struggling with this - I've added the DefaultIgnoreCondition to preserve pre-1.0 behaviour per the docs but I'm seeing the issue described: deserialisation succeeding with null properties where it should have failed.

Tarmil commented 1 year ago

The new option SkippableOptionFields was added in v1.1 for this use case. https://github.com/Tarmil/FSharp.SystemTextJson/blob/master/docs/Customizing.md#skippable-option-fields