diff --git a/pyhocon/config_tree.py b/pyhocon/config_tree.py index c3bbd277..b854ddca 100644 --- a/pyhocon/config_tree.py +++ b/pyhocon/config_tree.py @@ -58,7 +58,8 @@ def merge_configs(a, b, copy_trees=False): if isinstance(value, ConfigValues): value.parent = a value.key = key - value.overriden_value = a.get(key, None) + if key in a: + value.overriden_value = a[key] a[key] = value if a.root: a.history[key] = (a.history.get(key) or []) + b.history.get(key) diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index f388e8ce..6b25e712 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -1277,6 +1277,21 @@ def test_substitution_nested_override(self): assert config['database.name'] == 'peopledb' assert config['database.pass'] == 'peoplepass' + def test_optional_with_merge(self): + unresolved = ConfigFactory.parse_string( + """ + foo: 42 + foo: ${?a} + """, resolve=False) + source = ConfigFactory.parse_string( + """ + b: 14 + """) + config = unresolved.with_fallback(source) + assert config['foo'] == 42 + config = source.with_fallback(unresolved) + assert config['foo'] == 42 + def test_optional_substitution(self): config = ConfigFactory.parse_string( """