From f4b4f50cfddf81465b128cb6c67709b9c1a6f4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 4 Apr 2022 19:02:44 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20handling=20of=20invalid=20?= =?UTF-8?q?intermittend=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We not only ignore None values on our subkey paths, we ignore anything that isn't a dict we can further level down in. Closes #4479 --- src/octoprint/settings.py | 18 ++++++++++++++++-- tests/settings/test_settings.py | 4 ++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 8beb349ce..649090240 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -577,16 +577,30 @@ class HierarchicalChainMap: if d is None: return {} - result = dict() + result = {} for key, value in d.items(): if not key.startswith(prefix): continue subkeys = key[len(prefix) :].split(_CHAINMAP_SEP) current = result + + path = [] for subkey in subkeys[:-1]: - if subkey not in current or current[subkey] is None: + # we only need that for logging in case of data weirdness below + path.append(subkey) + + # make sure the subkey is in the current dict, and that it is a dict + if subkey not in current: current[subkey] = {} + elif not isinstance(current[subkey], dict): + logging.getLogger(__name__).warning( + f"There is a non-dict value on the path to {key} at {path!r}, ignoring." + ) + current[subkey] = {} + + # go down a level current = current[subkey] + current[subkeys[-1]] = value return result diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 468589c82..4192ec1fc 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -791,6 +791,10 @@ class ChainmapTest(unittest.TestCase): {_key("a"): None, _key("a", "b"): "b"}, {"a": {"b": "b"}}, ), + ( + {_key("a"): "", _key("a", "b"): "b"}, + {"a": {"b": "b"}}, + ), ) @ddt.unpack def test_unflatten(self, value, expected):