From 2ff381d7f57c99ee7070b123a126531d682042a4 Mon Sep 17 00:00:00 2001 From: Bram Date: Fri, 26 Apr 2024 15:26:52 +0200 Subject: [PATCH] Fix JSON comparison bug for non-comparable types --- src/Internal/Tools/Json.elm | 8 ++++++-- tests/Test/Values/Room.elm | 12 +++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index cf31b4f..28ccd8c 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -434,12 +434,16 @@ field = , description = description , encoder = \o -> + let + v = + encoder o + in -- If the value matches the default, do not record - if o == Tuple.first default then + if E.encode 0 v == E.encode 0 (encoder (Tuple.first default)) then Nothing else - Maybe.Just (encoder o) + Maybe.Just v , decoder = D.opFieldWithDefault fieldName default decoder , docs = docs , requiredness = diff --git a/tests/Test/Values/Room.elm b/tests/Test/Values/Room.elm index afab64f..3a1e747 100644 --- a/tests/Test/Values/Room.elm +++ b/tests/Test/Values/Room.elm @@ -89,11 +89,17 @@ suite = , fuzz fuzzer "Room -> JSON -> Room is equal" (\room -> - room - |> Room.encode + let + value : E.Value + value = + Room.encode room + in + value |> D.decodeValue Room.decode |> Result.toMaybe |> Maybe.map Tuple.first - |> Expect.equal (Just room) + |> Maybe.map Room.encode + |> Maybe.map (E.encode 0) + |> Expect.equal (Just <| E.encode 0 value) ) ]