thoth-org / Thoth.Json

Library for working with JSON in a type safe manner, this libs is targeting Fable

Home Page:https://thoth-org.github.io/Thoth.Json/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generateDecoderCached returns the same decoder for different types

kerams opened this issue · comments

let a = """[[ "blah", 0 ]]"""
let b = """[[ 0, "blah" ]]"""
printfn "%A" (Decode.fromString (Decode.Auto.generateDecoderCached<(string * int)[]> ()) a)
printfn "%A" (Decode.fromString (Decode.Auto.generateDecoderCached<(int * string)[]> ()) b)

The second decode fails unless the first one is commented out.

The problem is here:

        static member generateBoxedDecoderCached(t: System.Type, ?caseStrategy : CaseStrategy, ?extra: ExtraCoders): BoxedDecoder =
            let caseStrategy = defaultArg caseStrategy PascalCase

            let key =
                t.FullName
                |> (+) (Operators.string caseStrategy)
                |> (+) (extra |> Option.map (fun e -> e.Hash) |> Option.defaultValue "")

            Util.CachedDecoders.GetOrAdd(key, fun _ -> autoDecoder (makeExtra extra) caseStrategy false t)

        static member inline generateDecoderCached<'T>(?caseStrategy : CaseStrategy, ?extra: ExtraCoders): Decoder<'T> =
            Auto.generateBoxedDecoderCached(typeof<'T>, ?caseStrategy=caseStrategy, ?extra=extra) |> unboxDecoder

t.FullName returns System.Tuple`2[] for both types. typeof<'T>.FullName in generateDecoderCached would return a string with generic type arguments plugged in, i.e. System.Tuple`2[System.String,System.Int32][].

@alfonsogarciacaro Do you know what is happening here and if it is a Fable bug?

It looks indeed like a Fable bug :/ I'll try to fix it.

All good with 3.4.9, many thanks to you both.

Thank you @kerams for taking the time to identify the cause on Gitter.

Thank you @alfonsogarciacaro for the quick fix :)