From ceea1681fcdc13bd20b67412c47481399e442b39 Mon Sep 17 00:00:00 2001 From: Alex Bullen Date: Tue, 2 Aug 2022 16:50:50 -0700 Subject: [PATCH 1/2] Fix to ensure Remove operation paths are escaped when diffing --- lib/jsonpatch.ex | 2 +- test/jsonpatch_test.exs | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/jsonpatch.ex b/lib/jsonpatch.ex index 7ccc815..6dcb0a1 100644 --- a/lib/jsonpatch.ex +++ b/lib/jsonpatch.ex @@ -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) diff --git a/test/jsonpatch_test.exs b/test/jsonpatch_test.exs index a9f7c0e..69b68fe 100644 --- a/test/jsonpatch_test.exs +++ b/test/jsonpatch_test.exs @@ -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"} @@ -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}]}]} From 36642b4509100194bf858d491d9a3c3e584fb3a5 Mon Sep 17 00:00:00 2001 From: Alex Bullen Date: Wed, 3 Aug 2022 10:08:11 -0700 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 7b8c743..96a9bf1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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