diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index fc3e60828..8e2de8375 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -6009,6 +6009,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = j.cbegin(); it != j.cend(); ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8959265da..4bae31ac9 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23414,6 +23414,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = j.cbegin(); it != j.cend(); ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 8d0175e9d..2dba07976 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -679,6 +679,23 @@ TEST_CASE("regression tests 2") test3[json::json_pointer(p)] = json::object(); CHECK(test3.dump() == "{\"/root\":{}}"); } + + SECTION("issue #3007 - Parent pointers properly set when using update()") + { + json j = json::object(); + json lower = json::object(); + + { + json j2 = json::object(); + j2["one"] = 1; + + j.update(j2); + } + + for (auto const & kv : j) { + CHECK(kv.m_parent == &j); + } + } } DOCTEST_CLANG_SUPPRESS_WARNING_POP