Skip to content

Commit de7ea3b

Browse files
committed
fix: enhance merge logic to handle undefined target values and add corresponding unit tests
1 parent 93b0b61 commit de7ea3b

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/qs_codec/utils/utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,16 @@ def merge(
328328
has_source = idx in frame.list_source
329329

330330
if has_target and has_source:
331+
target_value = frame.list_target[idx]
332+
source_value = frame.list_source[idx]
333+
334+
if isinstance(source_value, Undefined):
335+
if not isinstance(target_value, Undefined):
336+
frame.list_merged.append(target_value)
337+
continue
338+
331339
frame.phase = "list_wait_child"
332-
stack.append(
333-
_MergeFrame(target=frame.list_target[idx], source=frame.list_source[idx], options=frame.options)
334-
)
340+
stack.append(_MergeFrame(target=target_value, source=source_value, options=frame.options))
335341
continue
336342

337343
if has_target:

tests/unit/utils_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,24 @@ def test_merges_array_into_object(self) -> None:
594594
{"foo": ["xyzzy"]},
595595
) == {"foo": {"bar": "baz", "0": "xyzzy"}}
596596

597+
def test_merge_mapping_target_with_scalar_source_returns_target_unchanged(self) -> None:
598+
target = {"a": "b"}
599+
source = "scalar"
600+
601+
result = Utils.merge(target, source) # type: ignore[arg-type]
602+
603+
assert result == {"a": "b"}
604+
assert result is target
605+
606+
def test_merge_structured_lists_prefers_source_when_target_slot_is_undefined(self) -> None:
607+
options = DecodeOptions()
608+
target = [Undefined()]
609+
source = [{"from_source": 1}]
610+
611+
result = Utils.merge(target, source, options)
612+
613+
assert result == [{"from_source": 1}]
614+
597615
def test_merge_deep_maps_without_stack_overflow(self) -> None:
598616
# Keep this above common recursion limits so recursion regressions still fail quickly.
599617
depth = 12_000

0 commit comments

Comments
 (0)