Skip to content

Fix: update() parent pointers not updated after recursive merge with JSON_DIAGNOSTICS#5187

Merged
nlohmann merged 3 commits into
nlohmann:developfrom
VasuBhakt:develop
May 22, 2026
Merged

Fix: update() parent pointers not updated after recursive merge with JSON_DIAGNOSTICS#5187
nlohmann merged 3 commits into
nlohmann:developfrom
VasuBhakt:develop

Conversation

@VasuBhakt
Copy link
Copy Markdown
Contributor

Issue

Fixes #4813

Overview

Added a set_parents() call under #if JSON_DIAGNOSTICS in the update() iterator overload after a recursive merge.

When update() is called with merge_objects=true, nested objects are recursively merged via it2->second.update(it.value(), true). After this recursive call, the child nodes inside the merged object have stale m_parent pointers, causing the parent consistency assertion to fire when JSON_DIAGNOSTICS=1 is enabled. Calling set_parents() after the recursive merge walks the subtree and fixes all parent pointers correctly.

Changes

  • include/nlohmann/json.hpp — 3 line fix in update() iterator overload
  • single_include/nlohmann/json.hpp — regenerated via make amalgamate

The changes were tested locally which gave the results:

Screenshot 2026-05-21 193726
  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

@VasuBhakt VasuBhakt requested a review from nlohmann as a code owner May 21, 2026 15:16
@github-actions github-actions Bot added the S label May 21, 2026
@VasuBhakt VasuBhakt changed the title Fix: update() with merge_objects=true triggers JSON_ASSERT when JSON_DIAGNOSTICS is enabled Fix: update() parent pointers not updated after recursive merge with JSON_DIAGNOSTICS May 21, 2026
Copy link
Copy Markdown
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a regression test (best the example from #4813) to unit-regression2.cpp?

@nlohmann nlohmann added this to the Release 3.12.1 milestone May 21, 2026
@VasuBhakt
Copy link
Copy Markdown
Contributor Author

sure, working on it.

@github-actions github-actions Bot added M tests and removed S labels May 22, 2026
VasuBhakt added 2 commits May 22, 2026 06:33
Signed-off-by: VasuBhakt <cpswastik31@gmail.com>
Signed-off-by: VasuBhakt <cpswastik31@gmail.com>
@VasuBhakt
Copy link
Copy Markdown
Contributor Author

added the regression test @nlohmann, please review it

Comment thread tests/src/unit-regression2.cpp Outdated
Comment thread tests/src/unit-regression2.cpp Outdated
Signed-off-by: VasuBhakt <cpswastik31@gmail.com>
Comment thread tests/src/unit-regression2.cpp Outdated
Copy link
Copy Markdown
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann merged commit 584e6b1 into nlohmann:develop May 22, 2026
143 checks passed
@VasuBhakt
Copy link
Copy Markdown
Contributor Author

Thanks a lot @nlohmann! You have been very patient and responsive throughout the review process. Really appreciate the guidance!

@nlohmann
Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

json::update() with merge_objects==true may trigger JSON_ASSERT for some objects

2 participants