Skip to content

Commit 7927e3b

Browse files
authored
Merge pull request #17 from stuartc/nils_and_missing_keys
Treat nil values differently from missing keys
2 parents 7b2154a + 587ab5d commit 7927e3b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/jsonpatch.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ defmodule Jsonpatch do
175175
acc =
176176
case get(source, key) do
177177
# Key is not present in source
178-
nil ->
178+
:__jsonpatch_lib__missing_value__ ->
179179
[%Add{path: current_path, value: val} | acc]
180180

181181
# Source has a different value but both (destination and source) value are lists or a maps
@@ -209,11 +209,11 @@ defmodule Jsonpatch do
209209

210210
# Unified access to lists or maps
211211
defp get(source, key) when is_list(source) do
212-
Enum.at(source, key)
212+
Enum.at(source, key, :__jsonpatch_lib__missing_value__)
213213
end
214214

215215
defp get(source, key) when is_map(source) do
216-
Map.get(source, key)
216+
Map.get(source, key, :__jsonpatch_lib__missing_value__)
217217
end
218218

219219
# Escape `/` to `~1 and `~` to `~0`.

test/jsonpatch_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ defmodule JsonpatchTest do
5050
assert [%Remove{path: "/baz"}] = patch
5151
end
5252

53+
test "Create no diff on unchanged nil object value" do
54+
source = %{"id" => nil}
55+
destination = %{"id" => nil}
56+
assert [] = Jsonpatch.diff(source, destination)
57+
end
58+
59+
test "Create no diff on unchanged array value" do
60+
source = [nil]
61+
destination = [nil]
62+
63+
assert [] = Jsonpatch.diff(source, destination)
64+
end
65+
5366
test "Create no diff on unexpected input" do
5467
assert [] = Jsonpatch.diff("unexpected", 1)
5568
end

0 commit comments

Comments
 (0)