Fix matching of integer keys in nested subdicts

This commit is contained in:
Erik Johnson 2020-04-20 15:23:29 -05:00 committed by Daniel Wozniak
parent 5a73ef699f
commit 22d3087e6b
2 changed files with 23 additions and 1 deletions

View file

@ -827,7 +827,21 @@ def traverse_dict_and_list(data, key, default=None, delimiter=DEFAULT_TARGET_DEL
else:
try:
ptr = ptr[each]
except (KeyError, TypeError):
except KeyError:
# Late import to avoid circular import
import salt.utils.args
# YAML-load the current key (catches integer dict keys)
try:
loaded_key = salt.utils.args.yamlify_arg(each)
except Exception: # pylint: disable=broad-except
return default
if loaded_key != each:
try:
ptr = ptr[loaded_key]
except (KeyError, TypeError):
return default
except TypeError:
return default
return ptr

View file

@ -226,6 +226,14 @@ class DataTestCase(TestCase):
{"not_found": "not_found"},
),
)
# Traverse and match integer key in a nested dict
# https://github.com/saltstack/salt/issues/56444
self.assertEqual(
"it worked",
salt.utils.data.traverse_dict_and_list(
{"foo": {1234: "it worked"}}, "foo:1234", "it didn't work",
),
)
def test_compare_dicts(self):
ret = salt.utils.data.compare_dicts(old={"foo": "bar"}, new={"foo": "bar"})