Skip to content

Commit

Permalink
fix #164: NRE on deserializing missing field with JsonIgnoreCondition…
Browse files Browse the repository at this point in the history
….WhenWritingNull
  • Loading branch information
Tarmil committed Jul 7, 2023
1 parent c08a9e3 commit fca5a58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/FSharp.SystemTextJson/Record.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type private RecordField

let isSkippableType = isSkippableType fsOptions p.PropertyType

let canBeSkipped = ignore || ignoreNullValues options || isSkippableType
let canBeSkipped = ignore || (ignoreNullValues options && nullValue.IsSome) || isSkippableType

let read =
let m = p.GetGetMethod()
Expand Down Expand Up @@ -226,7 +226,7 @@ type JsonRecordConverter<'T> internal (options: JsonSerializerOptions, fsOptions
| _ -> reader.Skip()
| _ -> ()

if requiredFieldCount < minExpectedFieldCount && not (ignoreNullValues options) then
if requiredFieldCount < minExpectedFieldCount then
for i in 0 .. fieldCount - 1 do
if isNull fields[i] && fieldProps[i].MustBePresent then
failf "Missing field for record type %s: %s" recordType.FullName fieldProps[i].Names[0]
Expand Down
10 changes: 8 additions & 2 deletions tests/FSharp.SystemTextJson.Tests/Test.Record.fs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,13 @@ module NonStruct =
Assert.Equal("""{"unignoredX":1}""", actual)

let ignoreNullOptions =
JsonFSharpOptions().ToJsonSerializerOptions(IgnoreNullValues = true)
JsonFSharpOptions()
.WithAllowNullFields()
.ToJsonSerializerOptions(IgnoreNullValues = true)

let newIgnoreNullOptions =
JsonFSharpOptions()
.WithAllowNullFields()
.ToJsonSerializerOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)

[<AllowNullLiteral>]
Expand Down Expand Up @@ -763,10 +766,13 @@ module Struct =
Assert.Equal("""{"unignoredX":1}""", actual)

let ignoreNullOptions =
JsonFSharpOptions().ToJsonSerializerOptions(IgnoreNullValues = true)
JsonFSharpOptions()
.WithAllowNullFields()
.ToJsonSerializerOptions(IgnoreNullValues = true)

let newIgnoreNullOptions =
JsonFSharpOptions()
.WithAllowNullFields()
.ToJsonSerializerOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)

[<AllowNullLiteral>]
Expand Down

0 comments on commit fca5a58

Please sign in to comment.