Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.0.1 (provisional)
- Escape remaining keys before comparing them to the (already escaped) keys from earlier in the diffing process when determining Remove operations

# 1.0.0
- Allow lists at top level of Jsonpatch.apply_patch
- Fix error message when updating a non existing key in list
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonpatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ defmodule Jsonpatch do
acc =
source
|> flat()
|> Stream.map(fn {k, _} -> k end)
|> Stream.map(fn {k, _} -> escape(k) end)
|> Stream.filter(fn k -> k not in checked_keys end)
|> Stream.map(fn k -> %Remove{path: "#{ancestor_path}/#{k}"} end)
|> Enum.reduce(acc, fn r, acc -> [r | acc] end)
Expand Down
21 changes: 20 additions & 1 deletion test/jsonpatch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule JsonpatchTest do
] = patch
end

test "Create diff with escaped '~' and '/' in path" do
test "Create diff with escaped '~' and '/' in path when adding" do
source = %{}
destination = %{"escape/me~now" => "somnevalue"}

Expand All @@ -117,6 +117,25 @@ defmodule JsonpatchTest do
actual_patch
end

test "Create diff with escaped '~' and '/' in path when removing" do
source = %{"escape/me~now" => "somnevalue"}
destination = %{}

actual_patch = Jsonpatch.diff(source, destination)

assert [%Jsonpatch.Operation.Remove{path: "/escape~1me~0now"}] = actual_patch
end

test "Create diff with escaped '~' and '/' in path when replacing" do
source = %{"escape/me~now" => "somnevalue"}
destination = %{"escape/me~now" => "othervalue"}

actual_patch = Jsonpatch.diff(source, destination)

assert [%Jsonpatch.Operation.Replace{path: "/escape~1me~0now", value: "othervalue"}] =
actual_patch
end

test "Create diff with nested map with correct Add/Remove order" do
source = %{"a" => [%{"b" => []}]}
target = %{"a" => [%{"b" => [%{"c" => 1}, %{"d" => 2}]}]}
Expand Down