Fixed patch::add creating non-existent parents.

Previous behavior was not in accordance with RFC6902. Added test case to unit-json_patch.cpp that catches the bug.
This commit is contained in:
Hudson00 2021-12-16 20:24:02 -05:00
parent 760304635d
commit 3357976b7e
3 changed files with 12 additions and 2 deletions

View File

@ -8456,7 +8456,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// get reference to parent of JSON pointer ptr
const auto last_path = ptr.back();
ptr.pop_back();
basic_json& parent = result[ptr];
// parent must exist when performing patch add per RFC6902 specs
basic_json& parent = result.at(ptr);
switch (parent.m_type)
{

View File

@ -25996,7 +25996,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// get reference to parent of JSON pointer ptr
const auto last_path = ptr.back();
ptr.pop_back();
basic_json& parent = result[ptr];
// parent must exist when performing patch add per RFC6902 specs
basic_json& parent = result.at(ptr);
switch (parent.m_type)
{

View File

@ -83,6 +83,14 @@ TEST_CASE("JSON patch")
CHECK_THROWS_AS(doc2.patch(patch), json::out_of_range&);
CHECK_THROWS_WITH(doc2.patch(patch),
"[json.exception.out_of_range.403] key 'a' not found");
json doc3 = R"({ "a": {} })"_json;
json patch3 = R"([{ "op": "add", "path": "/a/b/c", "value": 1 }])"_json;
// patch3 should cause an error because "b" does not exist in doc3
CHECK_THROWS_AS(doc3.patch(patch3), json::out_of_range&);
CHECK_THROWS_WITH(doc3.patch(patch3),
"[json.exception.out_of_range.403] key 'b' not found");
}
SECTION("4.2 remove")