Skip to content

Commit

Permalink
init additionalProperties in schema for defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Apr 9, 2021
1 parent 3543ad1 commit e1ef44b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions st2common/st2common/util/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion st2common/tests/unit/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e1ef44b

Please sign in to comment.