diff --git a/README.md b/README.md index cb05d744b..9f0737fb0 100644 --- a/README.md +++ b/README.md @@ -502,7 +502,11 @@ $ make $ ./json_unit "*" =============================================================================== +<<<<<<< HEAD +All tests passed (5568722 assertions in 32 test cases) +======= All tests passed (5568721 assertions in 32 test cases) +>>>>>>> develop ``` For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml). diff --git a/src/json.hpp b/src/json.hpp index 9d6687ddc..c514015a5 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -10002,9 +10002,12 @@ basic_json_parser_63: // in a second pass, traverse the remaining elements // remove my remaining elements + const auto end_index = static_cast(result.size()); while (i < source.size()) { - result.push_back(object( + // add operations in reverse order to avoid invalid + // indices + result.insert(result.begin() + end_index, object( { {"op", "remove"}, {"path", path + "/" + std::to_string(i)} diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 9681bed2f..86d901145 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -9312,9 +9312,12 @@ class basic_json // in a second pass, traverse the remaining elements // remove my remaining elements + const auto end_index = static_cast(result.size()); while (i < source.size()) { - result.push_back(object( + // add operations in reverse order to avoid invalid + // indices + result.insert(result.begin() + end_index, object( { {"op", "remove"}, {"path", path + "/" + std::to_string(i)} diff --git a/test/src/unit.cpp b/test/src/unit.cpp index e1c0b4c60..2fc0f5b2b 100644 --- a/test/src/unit.cpp +++ b/test/src/unit.cpp @@ -14107,6 +14107,15 @@ TEST_CASE("regression tests") CHECK(data == json({{"key", "value"}, {"key2", "value2"}, {"key3", "value3"}})); } + + SECTION("issue #269 - diff generates incorrect patch when removing multiple array elements") + { + json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json; + json expected = R"( { "arr1": [1, 2] } )"_json; + + // check roundtrip + CHECK(doc.patch(json::diff(doc, expected)) == expected); + } } // special test case to check if memory is leaked if constructor throws