Tarmil / FSharp.SystemTextJson

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

Fixes unit field deserialization #64

Closed NickDarvey closed 3 years ago

NickDarvey commented 3 years ago

I was getting the error: Unexpected null inside tuple-array. Expected type Unit, but got null. when deserializing a type like int * unit. This PR removes null-checking for unit types inside a tuple and adds a test.

Use case

It's an odd thing to serialize, but I use TenantId * RequestParameters across all my Bolero IRemoteService endpoints. In a couple of places, there are no RequestParameters so I use TenantId * unit here to maintain a consistent shape. Without this PR, a _ * unit tuple serializes but does not deserialize.

Edit: A consistent shape is useful so I can write things like:

member inline this.Authorize<'req, 'resp when 'req : (member get_Item1 : unit -> TenantId)> (f : 'req -> Async<'resp>) : 'req -> Async<'resp> = ...
Tarmil commented 3 years ago

Good catch. This looks like it can be an issue in other situations than just tuples though. We should probably have a more general solution, such as having isNullableFieldType return true for unit.

Tarmil commented 3 years ago

Awesome, thanks! 👍