From e1ef44b1a0ee641b2673016313b70a0e48d10a59 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Thu, 8 Apr 2021 20:04:16 -0400 Subject: [PATCH] init additionalProperties in schema for defaults --- st2common/st2common/util/config_loader.py | 17 ++++++++++++++--- st2common/tests/unit/test_config_loader.py | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/st2common/st2common/util/config_loader.py b/st2common/st2common/util/config_loader.py index ef1d5b4c2e..cff48a03c8 100644 --- a/st2common/st2common/util/config_loader.py +++ b/st2common/st2common/util/config_loader.py @@ -101,12 +101,16 @@ def _get_values_for_config(self, config_schema_db, config_db): return config @staticmethod - def _get_object_property_schema(object_schema): + def _get_object_property_schema(object_schema, init_additional_properties=None): additional_properties = object_schema.get("additionalProperties", {}) - if isinstance(additional_properties, dict): + if additional_properties and isinstance(additional_properties, dict): property_schema = defaultdict(lambda: additional_properties) else: property_schema = {} + if init_additional_properties: + # ensure that these keys are present in the object (vs just defaultdict) + for key in init_additional_properties: + property_schema.__missing__(key) property_schema.update(object_schema.get("properties", {})) return property_schema @@ -206,7 +210,14 @@ def _assign_default_values(self, schema, config): if not config.get(schema_item_key, None): config[schema_item_key] = {} - property_schema = self._get_object_property_schema(schema_item) + property_schema = self._get_object_property_schema( + schema_item, + init_additional_properties=( + config[schema_item_key].keys() + if has_additional_properties + else None + ), + ) self._assign_default_values( schema=property_schema, config=config[schema_item_key] diff --git a/st2common/tests/unit/test_config_loader.py b/st2common/tests/unit/test_config_loader.py index 3927fd5a80..50c8f27f98 100644 --- a/st2common/tests/unit/test_config_loader.py +++ b/st2common/tests/unit/test_config_loader.py @@ -557,7 +557,7 @@ def test_get_config_dynamic_config_item_under_additional_properties(self): self.assertEqual( config_rendered, { - "regions": ["us-east-1"], + "regions": "us-east-1", "profiles": { "dev": { "host": "127.0.0.3",