Skip to content

Commit 2654ecb

Browse files
committed
Merge PR #306: Fix issue #305: Merge dotted keys when non-dotted key with same prefix follows (@jbemmel)
2 parents 1252fc4 + d079174 commit 2654ecb

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎box/box.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,20 @@ def __convert_and_store(self, item, value):
594594
return super().__setitem__(item, value)
595595
# This is the magic sauce that makes sub dictionaries into new box objects
596596
if isinstance(value, dict):
597+
# If box_dots is enabled and the key already exists as a Box (possibly created from dotted keys),
598+
# merge the new dict into the existing Box instead of overwriting it
599+
# This fixes issue #305 where dotted keys are lost when a non-dotted key with the same prefix comes after
600+
if (
601+
self._box_config["box_dots"]
602+
and item in self
603+
and isinstance(self[item], Box)
604+
and not self._box_config["__created"]
605+
):
606+
# Merge the new dict into the existing Box by setting each key-value pair
607+
# This ensures proper conversion of nested structures
608+
for k, v in value.items():
609+
self[item].__setitem__(k, v)
610+
return
597611
# We always re-create even if it was already a Box object to pass down configurations correctly
598612
value = self._box_config["box_class"](value, **self.__box_config(extra_namespace=item))
599613
elif isinstance(value, list) and not isinstance(value, box.BoxList):

‎test/test_box.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,54 @@ def test_from_yaml(self):
373373
assert bx.key1 == "value1"
374374
assert bx.Key_2 == Box()
375375

376+
def test_from_yaml_box_dots_dotted_key_before_non_dotted(self):
377+
"""
378+
Test for issue #305: Dotted keys read from YAML are lost with box_dots / default_box
379+
when the dotted key appears before a non-dotted key with the same prefix.
380+
381+
This test documents the bug where a.b: c followed by a: {d: e} causes a.b to be lost.
382+
"""
383+
# Create a temporary YAML file with dotted key before non-dotted key
384+
yaml_content = """a.b: c
385+
a:
386+
d: e
387+
"""
388+
yaml_file = Path(tmp_dir, "test_dotted_before.yml")
389+
yaml_file.write_text(yaml_content)
390+
391+
# Test without box_dots/default_box - should preserve both keys as separate entries
392+
bx1 = Box.from_yaml(filename=yaml_file)
393+
assert "a.b" in bx1
394+
assert bx1["a.b"] == "c"
395+
assert "a" in bx1
396+
assert bx1["a"]["d"] == "e"
397+
398+
# Test with box_dots=True and default_box=True
399+
# Expected behavior: a.b should be merged into a.b, so a.b == "c" and a.d == "e"
400+
# Current bug: a.b is lost when it appears before a:
401+
bx2 = Box.from_yaml(filename=yaml_file, box_dots=True, default_box=True)
402+
assert "a" in bx2
403+
assert bx2["a"]["d"] == "e"
404+
# This assertion will fail until the bug is fixed:
405+
# The dotted key a.b should be accessible as either a["b"] or via dotted notation
406+
assert "b" in bx2["a"], "Bug #305: a.b key is lost when it appears before a: in YAML"
407+
assert bx2["a"]["b"] == "c"
408+
409+
# Test reverse order (non-dotted before dotted) - this should work correctly
410+
yaml_content_reverse = """a:
411+
d: e
412+
a.b: c
413+
"""
414+
yaml_file_reverse = Path(tmp_dir, "test_dotted_after.yml")
415+
yaml_file_reverse.write_text(yaml_content_reverse)
416+
417+
bx3 = Box.from_yaml(filename=yaml_file_reverse, box_dots=True, default_box=True)
418+
assert "a" in bx3
419+
assert bx3["a"]["d"] == "e"
420+
# In reverse order, the dotted key should be properly merged
421+
assert "b" in bx3["a"]
422+
assert bx3["a"]["b"] == "c"
423+
376424
def test_bad_from_json(self):
377425
with pytest.raises(BoxError):
378426
Box.from_json()

0 commit comments

Comments
 (0)