From dd04a32918c097aaabdf93fc76df43641fd3d8c4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 13 Apr 2020 14:35:36 +0200 Subject: [PATCH] :bug: fix bug in diff function #1983 --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-regression.cpp | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 88b550f3..4f8701a3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7930,7 +7930,7 @@ class basic_json result.push_back( { {"op", "add"}, - {"path", path + "/" + std::to_string(i)}, + {"path", path + "/-"}, {"value", target[i]} }); ++i; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 15d069fc..29d4439d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22741,7 +22741,7 @@ class basic_json result.push_back( { {"op", "add"}, - {"path", path + "/" + std::to_string(i)}, + {"path", path + "/-"}, {"value", target[i]} }); ++i; diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index f5139264..78699ad3 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1896,6 +1896,14 @@ TEST_CASE("regression tests") static_assert(!std::is_constructible>::value, ""); static_assert(std::is_constructible>::value, ""); } + + SECTION("issue #1983 - JSON patch diff for op=add formation is not as per standard (RFC 6902)") + { + const auto source = R"({ "foo": [ "1", "2" ] })"_json; + const auto target = R"({"foo": [ "1", "2", "3" ]})"_json; + const auto result = json::diff(source, target); + CHECK(result.dump() == R"([{"op":"add","path":"/foo/-","value":"3"}])"); + } } #if not defined(JSON_NOEXCEPTION)