json
json copied to clipboard
Json equality for oneOf decoders always returns true
The oneOf
decoder stores an Elm list of decoders, but when comparing for equality these lists are compared with _Json_listEquality
, which compares JS arrays. The result is that oneOf
decoders always compare as equal.
I noticed this when a custom event decoder was not being replaced as I expected. Here's an SSCCE that demonstrates such a case.
module Main exposing (main)
import Browser
import Html exposing (Html)
import Html.Events
import Json.Decode
main : Program () Int Int
main =
Browser.sandbox
{ init = 0
, view = view
, update = always
}
view : Int -> Html Int
view clicks =
Html.button
[ Html.Events.on "click"
-- good:
--(Json.Decode.succeed (clicks + 1))
--
-- bad:
(Json.Decode.oneOf [ Json.Decode.succeed (clicks + 1) ])
]
[ Html.text (String.fromInt clicks)
]
This is a duplicate of https://github.com/elm/json/issues/13, although this is a good SSCCE!
There is a patch for this here: https://github.com/elm/core/pull/905
Which is old enough that it predates the Json stuff being split into a separate package from core.
I commented on it to ask the original author to re-create it on this repo, since it looks like a good patch and still relevant. If the author does not response, I will make a PR out of it on this repo.