Repository navigation
Fix merge(REPLACE) silently dropping source data for mappings with an empty-string key - #217
Open
TrueFurina wants to merge 1 commit into
Open
TrueFurina wants to merge 1 commit into
TrueFurina wants to merge 1 commit into
Conversation
… empty-string key merge(dst, src, flags=MergeType.REPLACE) used `target[""]` to detect whether the destination node was a mapping. On a mapping that raised KeyError (or, when the mapping actually contained an empty-string key, *succeeded*) the path was silently skipped, so the source data at that path was dropped with no error. Replace the probe with an explicit isinstance() check: mappings are always deep-merged (matching the existing default/dict behavior, so existing keys including an empty-string key are preserved), and only non-mapping targets (e.g. sequences) are replaced wholesale. This keeps the documented REPLACE-for-lists behavior and the default dict deep-merge unchanged. Adds a regression test (test_merge_replace_empty_string_key_in_dst). Co-authored-by: LLM
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
dpath.merge(dst, src, flags=dpath.MergeType.REPLACE)usedtarget[""]to decidewhether the destination node was a mapping. That probe was wrong in two ways:/n
KeyError, which was caught and fell through to arecursive merge (acceptable, but incidental).
target[""]succeeded,so the
exceptbranches never ran and the path was silently skipped -- thesource data at that path was dropped with no error.
Reproduction (before the fix)
Fix
Replace the probe with an explicit
isinstance(target, MutableMapping)check:/n* mappings are always deep-merged (existing keys, including an empty-string key,are preserved);
This leaves the documented
REPLACEbehavior for lists and the default dictdeep-merge unchanged. A regression test (
test_merge_replace_empty_string_key_in_dst)is included; the full suite (80 tests) passes.