Throw out_of_range.411 instead of asserting on JSON Patch "add" to a primitive parent#5222
Merged
Conversation
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
gregmarr
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A JSON Patch
addwhose target location has a parent that is neither an object nor an array (e.g.add/xyz/1/lmnwhere/xyz/1is a string) previously hit an internalJSON_ASSERT(false)inpatch_inplace. This meant:SIGABRT), andNDEBUG) 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
operation_addwith a new exception codeout_of_range.411: "cannot add value: the JSON Patch 'add' target's parent is of type , but must be an object or array" (include/andsingle_include/).out_of_range.411inexceptions.md, and add Exceptions + Version history entries topatch.md/patch_inplace.md.unit-json_patch.cpp(string/number parent, plus the original two-step sequence from the issue) and a positions-aware exact-message test inunit-diagnostic-positions.cpp.Notes
out_of_range.411) is introduced; behavior for previously-valid patches is unchanged.JSON_DiagnosticsOFF and ON; the debug-abort and the release silent-no-op are both eliminated.