Skip to content

Commit 487d895

Browse files
authored
Merge pull request #14 from tinfoil/bullen/bugfix
Incorrect escape on diff fix
2 parents ccb4146 + 18b342d commit 487d895

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.0.1 (provisional)
2+
- Escape remaining keys before comparing them to the (already escaped) keys from earlier in the diffing process when determining Remove operations
3+
14
# 1.0.0
25
- Allow lists at top level of Jsonpatch.apply_patch
36
- Fix error message when updating a non existing key in list

lib/jsonpatch.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ defmodule Jsonpatch do
160160
acc =
161161
source
162162
|> flat()
163-
|> Stream.map(fn {k, _} -> k end)
163+
|> Stream.map(fn {k, _} -> escape(k) end)
164164
|> Stream.filter(fn k -> k not in checked_keys end)
165165
|> Stream.map(fn k -> %Remove{path: "#{ancestor_path}/#{k}"} end)
166166
|> Enum.reduce(acc, fn r, acc -> [r | acc] end)

test/jsonpatch_test.exs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ defmodule JsonpatchTest do
107107
] = patch
108108
end
109109

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

@@ -117,6 +117,25 @@ defmodule JsonpatchTest do
117117
actual_patch
118118
end
119119

120+
test "Create diff with escaped '~' and '/' in path when removing" do
121+
source = %{"escape/me~now" => "somnevalue"}
122+
destination = %{}
123+
124+
actual_patch = Jsonpatch.diff(source, destination)
125+
126+
assert [%Jsonpatch.Operation.Remove{path: "/escape~1me~0now"}] = actual_patch
127+
end
128+
129+
test "Create diff with escaped '~' and '/' in path when replacing" do
130+
source = %{"escape/me~now" => "somnevalue"}
131+
destination = %{"escape/me~now" => "othervalue"}
132+
133+
actual_patch = Jsonpatch.diff(source, destination)
134+
135+
assert [%Jsonpatch.Operation.Replace{path: "/escape~1me~0now", value: "othervalue"}] =
136+
actual_patch
137+
end
138+
120139
test "Create diff with nested map with correct Add/Remove order" do
121140
source = %{"a" => [%{"b" => []}]}
122141
target = %{"a" => [%{"b" => [%{"c" => 1}, %{"d" => 2}]}]}

0 commit comments

Comments
 (0)