json icon indicating copy to clipboard operation
json copied to clipboard

Json equality for oneOf decoders always returns true

Open justinmimbs opened this issue 5 years ago • 2 comments

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)
        ]

justinmimbs avatar Apr 10 '19 16:04 justinmimbs

This is a duplicate of https://github.com/elm/json/issues/13, although this is a good SSCCE!

DavidDTA avatar May 08 '19 09:05 DavidDTA

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.

rupertlssmith avatar Mar 04 '22 15:03 rupertlssmith