Skip to content

Commit 6d59d97

Browse files
anujkumar93darthbear
authored andcommitted
Fix self references when environment variables exist (#219)
1 parent 0ded0b3 commit 6d59d97

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pyhocon/config_parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def parse_file(cls, filename, encoding='utf-8', required=True, resolve=True, unr
8989
:param unresolved_value: assigned value value to unresolved substitution.
9090
If overriden with a default value, it will replace all unresolved value to the default value.
9191
If it is set to to pyhocon.STR_SUBSTITUTION then it will replace the value by its substitution expression (e.g., ${x})
92-
:type unresolved_value: boolean
92+
:type unresolved_value: class
9393
:return: Config object
9494
:type return: Config
9595
"""
@@ -484,13 +484,18 @@ def _fixup_self_references(cls, config, accept_unresolved=False):
484484
_, _, current_item = cls._do_substitute(substitution, value)
485485
previous_item = current_item
486486

487-
if len(history) == 1: # special case, when self optional referencing without existing
487+
if len(history) == 1:
488488
for substitution in cls._find_substitutions(previous_item):
489489
prop_path = ConfigTree.parse_key(substitution.variable)
490490
if len(prop_path) > 1 and config.get(substitution.variable, None) is not None:
491491
continue # If value is present in latest version, don't do anything
492-
if prop_path[0] == key and substitution.optional:
493-
cls._do_substitute(substitution, None)
492+
if prop_path[0] == key:
493+
value = os.environ.get(key)
494+
if value is not None:
495+
cls._do_substitute(substitution, value)
496+
continue
497+
if substitution.optional: # special case, when self optional referencing without existing
498+
cls._do_substitute(substitution, None)
494499

495500
# traverse config to find all the substitutions
496501
@classmethod

tests/test_config_parser.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,26 @@ def test_string_from_environment(self):
21062106
'string_from_env': 'value_from_environment'
21072107
}
21082108

2109+
@mock.patch.dict(os.environ, STRING_VAR='value_from_environment')
2110+
def test_string_from_environment_self_ref(self):
2111+
config = ConfigFactory.parse_string(
2112+
"""
2113+
STRING_VAR = ${STRING_VAR}
2114+
""")
2115+
assert config == {
2116+
'STRING_VAR': 'value_from_environment'
2117+
}
2118+
2119+
@mock.patch.dict(os.environ, STRING_VAR='value_from_environment')
2120+
def test_string_from_environment_self_ref_optional(self):
2121+
config = ConfigFactory.parse_string(
2122+
"""
2123+
STRING_VAR = ${?STRING_VAR}
2124+
""")
2125+
assert config == {
2126+
'STRING_VAR': 'value_from_environment'
2127+
}
2128+
21092129
@mock.patch.dict(os.environ, TRUE_OR_FALSE='false')
21102130
def test_bool_from_environment(self):
21112131
config = ConfigFactory.parse_string(

0 commit comments

Comments
 (0)