🚑 fix for #894

- Implemented "copy" in terms of "add".
- Added check for JSON Pointer array indices to make sure the complete reference token was processed.
- Added test suite from https://github.com/json-patch/json-patch-tests
This commit is contained in:
Niels Lohmann 2017-12-28 13:52:23 +01:00
parent 3113a52a7d
commit 3b3b6e8e69
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
6 changed files with 853 additions and 18 deletions

View file

@ -31,6 +31,8 @@ SOFTWARE.
#include "json.hpp"
using nlohmann::json;
#include <fstream>
TEST_CASE("JSON patch")
{
SECTION("examples from RFC 6902")
@ -1250,4 +1252,42 @@ TEST_CASE("JSON patch")
R"( [{"op": "test", "path": "/foo", "value": "bar"}] )"_json));
}
}
SECTION("Tests from github.com/json-patch/json-patch-tests")
{
for (auto filename :
{"test/data/json-patch-tests/spec_tests.json",
"test/data/json-patch-tests/tests.json"
})
{
CAPTURE(filename);
std::ifstream f(filename);
json suite = json::parse(f);
for (const auto& test : suite)
{
CAPTURE(test.value("comment", ""))
// skip tests marked as disabled
if (test.value("disabled", false))
{
continue;
}
const auto& doc = test["doc"];
const auto& patch = test["patch"];
if (test.count("error") == 0)
{
// if an expected value is given, use it; use doc otherwise
const auto& expected = test.value("expected", doc);
CHECK(doc.patch(patch) == expected);
}
else
{
CHECK_THROWS(doc.patch(patch));
}
}
}
}
}

View file

@ -1322,4 +1322,32 @@ TEST_CASE("regression tests")
j = ar;
ar = j;
}
SECTION("issue #894 - invalid RFC6902 copy operation succeeds")
{
auto model = R"({
"one": {
"two": {
"three": "hello",
"four": 42
}
}
})"_json;
CHECK_THROWS_AS(model.patch(R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json), json::out_of_range);
CHECK_THROWS_WITH(model.patch(R"([{"op": "move",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json),
"[json.exception.out_of_range.403] key 'a' not found");
CHECK_THROWS_AS(model.patch(R"([{"op": "copy",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json), json::out_of_range);
CHECK_THROWS_WITH(model.patch(R"([{"op": "copy",
"from": "/one/two/three",
"path": "/a/b/c"}])"_json),
"[json.exception.out_of_range.403] key 'a' not found");
}
}