From a26cf1e8fb437c72c11081257c7ae42fe540d650 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 12 Dec 2024 22:14:13 +0100 Subject: [PATCH 1/2] :bug: set parents after insert call --- include/nlohmann/json.hpp | 1 + single_include/nlohmann/json.hpp | 1 + tests/src/unit-diagnostics.cpp | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 763fd95783..8ec9720ee7 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3401,6 +3401,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); + set_parents(); } /// @brief updates a JSON object from another object, overwriting existing keys diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7979227a05..0164992148 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22948,6 +22948,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); + set_parents(); } /// @brief updates a JSON object from another object, overwriting existing keys diff --git a/tests/src/unit-diagnostics.cpp b/tests/src/unit-diagnostics.cpp index 345d6eee29..9df37f8a63 100644 --- a/tests/src/unit-diagnostics.cpp +++ b/tests/src/unit-diagnostics.cpp @@ -242,4 +242,22 @@ TEST_CASE("Regression tests for extended diagnostics") json const j_arr_copy = j_arr; } } + + SECTION("Regression test for issue #3915 - JSON_DIAGNOSTICS trigger assertion") + { + json j = json::object(); + j["root"] = "root_str"; + + json jj = json::object(); + jj["child"] = json::object(); + + // If do not push anything in object, then no assert will be produced + jj["child"]["prop1"] = "prop1_value"; + + // Push all properties of child in parent + j.insert(jj.at("child").begin(), jj.at("child").end()); + + // Here assert is generated when construct new json + json k(j); + } } From cb41608cb5102f99e83c93ab5839301e7013a795 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 12 Dec 2024 22:28:49 +0100 Subject: [PATCH 2/2] :rotating_light: fix warning --- tests/src/unit-diagnostics.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/src/unit-diagnostics.cpp b/tests/src/unit-diagnostics.cpp index 9df37f8a63..472d11e283 100644 --- a/tests/src/unit-diagnostics.cpp +++ b/tests/src/unit-diagnostics.cpp @@ -258,6 +258,8 @@ TEST_CASE("Regression tests for extended diagnostics") j.insert(jj.at("child").begin(), jj.at("child").end()); // Here assert is generated when construct new json - json k(j); + const json k(j); + + CHECK(k.dump() == "{\"prop1\":\"prop1_value\",\"root\":\"root_str\"}"); } }