Tarmil / FSharp.SystemTextJson

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

Omitting option fields in records throws #53

Closed ssiltanen closed 4 years ago

ssiltanen commented 4 years ago

There is actually a test case for this in FSharp.SystemTextJson/tests/FSharp.SystemTextJson.Tests/Test.Record.fs on row 73 where the issue can be reproduced: let allow omitting fields that are optional ()

The test is apparently not being run at the moment since it is missing the Fact attribute.


> let options = JsonSerializerOptions();;                                               
val options : JsonSerializerOptions

> options.Converters.Add(JsonFSharpConverter());;
val it : unit = ()

>     type SomeSearchApi =
-         {
-             filter: string option
-             limit: int option
-             offset: int option
-         };;
type SomeSearchApi =
  { filter: string option
    limit: int option
    offset: int option }

> let result  = JsonSerializer.Deserialize<SomeSearchApi>("""{}""", options);;
System.Text.Json.JsonException: Missing field for record type FSI_0042+SomeSearchApi
   at System.Text.Json.Serialization.JsonRecordConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in C:\projects\fsharp-systemtextjson\src\FSharp.SystemTextJson\Record.fs:line 105
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at <StartupCode$FSI_0043>.$FSI_0043.main@()```
ssiltanen commented 4 years ago

Sorry, my bad. I had older version running of this library and latest version had fixed this. Closing.