Skip to content

Throw out_of_range.411 instead of asserting on JSON Patch "add" to a primitive parent#5222

Merged
nlohmann merged 1 commit into
developfrom
fix/4292-patch-assert
Jul 1, 2026
Merged

Throw out_of_range.411 instead of asserting on JSON Patch "add" to a primitive parent#5222
nlohmann merged 1 commit into
developfrom
fix/4292-patch-assert

Conversation

@nlohmann

Copy link
Copy Markdown
Owner

Summary

A JSON Patch add whose target location has a parent that is neither an object nor an array (e.g. add /xyz/1/lmn where /xyz/1 is a string) previously hit an internal JSON_ASSERT(false) in patch_inplace. This meant:

  • debug builds aborted the process (SIGABRT), and
  • release builds (NDEBUG) silently dropped the operation, returning a wrong result with no error.

Per RFC 6902, such a target is invalid because its parent cannot receive a new member or element. This is now reported with a proper, catchable exception.

Fixes #4292.

Changes

  • Replace the unreachable-assertion branch in operation_add with a new exception code out_of_range.411: "cannot add value: the JSON Patch 'add' target's parent is of type , but must be an object or array" (include/ and single_include/).
  • Document out_of_range.411 in exceptions.md, and add Exceptions + Version history entries to patch.md / patch_inplace.md.
  • Add regression tests in unit-json_patch.cpp (string/number parent, plus the original two-step sequence from the issue) and a positions-aware exact-message test in unit-diagnostic-positions.cpp.

Notes

  • A new public exception id (out_of_range.411) is introduced; behavior for previously-valid patches is unchanged.
  • Verified green with JSON_Diagnostics OFF and ON; the debug-abort and the release silent-no-op are both eliminated.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann nlohmann added this to the Release 3.12.1 milestone Jun 30, 2026
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Jun 30, 2026
@nlohmann nlohmann removed the review needed It would be great if someone could review the proposed changes. label Jun 30, 2026
@nlohmann nlohmann merged commit 730b577 into develop Jul 1, 2026
142 checks passed
@nlohmann nlohmann deleted the fix/4292-patch-assert branch July 1, 2026 04:47
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.

Rel 3.11.2 -- patch method throw exception which use to work fine in Rel 3.9.1

2 participants