Skip to content

Disallow null if not option-wrapped everywhere, not just for record fields #158

@cmeeren

Description

@cmeeren

The document Customizing the serialization format says:

By default, FSharp.SystemTextJson throws an exception when the following conditions are met:

  • it is deserializing a record or a union;
  • a field's JSON value is null or the field is unspecified;
  • that field's type isn't an explicitly nullable F# type (like option, voption and Skippable).

What is the reason for the first requirement (emphasis mine)?

I ask because I was recently bitten by a bug in production. I am deserializing a JSON document to the type MyRootRecord, but when the JSON body itself is null, then the returned MyRootRecord is also null (even though it is a record and F# does not allow null values of it), causing some NullReferenceExecption elsewhere in the code.

I would expect that if I deserialize a top-level null value to a record/union (not wrapped in option or similar), then I get a deserialization exception similar to if it had been a record field.

In other words, the "throw on null" behavior should be governed by the target type, not by whether or not it's a property. (This could therefore also apply to array items, which I haven't tested.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions