Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unionTagName to be part of the record type definition when deserializing union of records #100

Closed
Zaid-Ajaj opened this issue Oct 25, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@Zaid-Ajaj
Copy link

Description

Right now, when deserializing a union where each case is a record, it appears that we cannot have the discriminator property be part of the record definition itself. The library throws an exception saying that the field is missing from the JSON, even though it is indeed available.

Reproduction

open System.Text.Json
open System.Text.Json.Serialization

let options = JsonSerializerOptions ()
let encoding =
    JsonUnionEncoding.InternalTag
    ||| JsonUnionEncoding.UnwrapRecordCases
    ||| JsonUnionEncoding.UnwrapFieldlessTags
    ||| JsonUnionEncoding.UnwrapOption
    ||| JsonUnionEncoding.AdjacentTag

// using __typename as discriminator
let converter = JsonFSharpConverter(encoding, unionTagName="__typename")
options.Converters.Add(converter)

type Company = { name: string }

type Employee = {
    __typeName: string
    firstName: string
}

[<RequireQualifiedAccess>]
type SearchResult =
    | Company of company: Company
    | Employee of employee: Employee

let companyJson = "{ \"__typename\": \"Company\", \"name\": \"McCompany\" }"
let employeeJson = "{ \"__typename\": \"Employee\", \"firstName\": \"John\"  }"

// Works
let company = JsonSerializer.Deserialize<SearchResult>(companyJson, options)
printfn "%A" company

// Fails
let employee = JsonSerializer.Deserialize<SearchResult>(employeeJson, options)
printfn "%A" employee

Error

Unhandled exception. System.Text.Json.JsonException: Missing field for record type Program+Employee: __typeName
   at System.Text.Json.Serialization.JsonRecordConverter`1.ReadRestOfObject(Utf8JsonReader& reader, JsonSerializerOptions options, Boolean skipFirstRead)
   at System.Text.Json.Serialization.JsonRecordConverter`1.System.Text.Json.Serialization.IRecordConverter.ReadRestOfObject(Utf8JsonReader& reader, JsonSerializerOptions options, Boolean skipFirstRead)

Expected

To be able to deserialize employeeJson as SearchResult and populate __typename with "Employee"

Actual

An exception is thrown

@Tarmil Tarmil added the bug Something isn't working label Oct 26, 2021
@Tarmil
Copy link
Owner

Tarmil commented Oct 26, 2021

I see, you want the "__typename" field to be used both as the tag for SearchResult and a field for Employee. It's not something I would have expected to be needed, but it's reasonable to expect it to work.

@Zaid-Ajaj
Copy link
Author

It's not something I would have expected to be needed, but it's reasonable to expect it to work.

It is required sometimes when working with Snowflaqe and configuring the serializer to use System.Text.Json. Right now, my workaround is to not generate the __typename field for the records but this isn't ideal when __typename is the only field that needs to be generated which is sometimes the case.

@Tarmil Tarmil closed this as completed in 4cbc82c Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants