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

Never ending recursion when trying to handle recursive discriminated unions. #147

Open
gdar91 opened this issue Jan 6, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@gdar91
Copy link

gdar91 commented Jan 6, 2023

There is a problem when it tries to process a discriminated union. If you have a union type parametrized by another Never-like union (type Never = Never of Never) the function responsible for calculating the null value - System.Text.Json.Serialization.Helpers.tryGetNullValue - enters into infinite recursion. Maybe it needs to keep track of visited types and do an early exit.

Exception:

...
   at System.Text.Json.Serialization.Helpers.tryGetNullValue(System.Text.Json.Serialization.JsonFSharpOptions, System.Type)
   at System.Text.Json.Serialization.Helpers.tryGetNullValue(System.Text.Json.Serialization.JsonFSharpOptions, System.Type)
...
...
...
   at System.Text.Json.Serialization.Helpers.tryGetNullValue(System.Text.Json.Serialization.JsonFSharpOptions, System.Type)
   at System.Text.Json.Serialization.Helpers.tryGetNullValue(System.Text.Json.Serialization.JsonFSharpOptions, System.Type)
   at <StartupCode$FSharp-SystemTextJson>.$Union.mapping@1-2[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Text.Json.JsonSerializerOptions, System.Text.Json.Serialization.JsonUnionConverter`1<System.__Canon>, System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Text.Json.Serialization.JsonName[]>, System.Collections.Generic.Dictionary`2<System.String,Int32>, System.Reflection.PropertyInfo, System.String, Int32)
   at <StartupCode$FSharp-SystemTextJson>.$Union.mapping@1-1[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.IDictionary`2<System.Type,System.Text.Json.Serialization.JsonFSharpOptions>, System.Text.Json.JsonSerializerOptions, System.Text.Json.Serialization.JsonUnionConverter`1<System.__Canon>, Microsoft.FSharp.Reflection.UnionCaseInfo)
...

The code to reproduce the error:

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


type Never = Never of Never

type MyChoice = MyChoice of Choice<int, Never>


let myChoice = MyChoice (Choice1Of2 10)


let options = JsonSerializerOptions ()

options.Converters.Add (JsonFSharpConverter ())


let json = JsonSerializer.Serialize (myChoice, options)


printfn "%s" json
@Tarmil Tarmil added the bug Something isn't working label Jan 14, 2023
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