fsprojects / FSharp.Data.JsonSchema

MIT License
48 stars 6 forks source link

Missing fields are deserialized to nulls #11

Closed hvester closed 3 years ago

hvester commented 3 years ago

If json does not contain a field corresponding to a property then I would assume the deserialization to fail instead of leaving the value of the property as null. I'm wondering if this is intentional or a bug?

#r "nuget: FSharp.Data.JsonSchema"

open FSharp.Data

type Foo = { Bar : string }

Json.Deserialize<Foo>("{}")
// val it : Foo = { Bar = null }

Seems to be caused by IgnoreNullValues=true in the DefaultOptions.

panesofglass commented 3 years ago

The System.Text.Json serializer currently allows things we may not allow in the JSON Schema. This isn't strictly wrong, as if you validated the JSON against the schema, it should fail. However, we could probably improve this such that even deserialization adheres to what we want.

panesofglass commented 3 years ago

Does #12 address your concern adequately?

hvester commented 3 years ago

I tested and it works as expected.

panesofglass commented 3 years ago

I've released this update in 2.0.0