Skip to content

Commit b7672ed

Browse files
committed
Simplify based on PR feedback
1 parent a2644c2 commit b7672ed

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Added
4343
Contributed by @Kami.
4444

4545
* Fix a bug in the pack config loader so that objects covered by an additionalProperties schema
46-
can use encrypted datastore keys and have their default values applied correctly.
46+
can use encrypted datastore keys and have their default values applied correctly. #5225
4747

4848
Contributed by @cognifloyd.
4949

st2common/st2common/util/config_loader.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from __future__ import absolute_import
1717
import copy
1818

19-
from collections import defaultdict
20-
2119
import six
2220

2321
from oslo_config import cfg
@@ -101,16 +99,19 @@ def _get_values_for_config(self, config_schema_db, config_db):
10199
return config
102100

103101
@staticmethod
104-
def _get_object_property_schema(object_schema, init_additional_properties=None):
102+
def _get_object_property_schema(object_schema, additional_properties_keys=None):
103+
"""
104+
Create a schema for an object property using both additionalProperties and properties.
105+
106+
:rtype: ``dict``
107+
"""
108+
property_schema = {}
105109
additional_properties = object_schema.get("additionalProperties", {})
110+
# additionalProperties can be a boolean or a dict
106111
if additional_properties and isinstance(additional_properties, dict):
107-
property_schema = defaultdict(lambda: additional_properties)
108-
else:
109-
property_schema = {}
110-
if init_additional_properties:
111-
# ensure that these keys are present in the object (vs just defaultdict)
112-
for key in init_additional_properties:
113-
property_schema.__missing__(key)
112+
# ensure that these keys are present in the object
113+
for key in additional_properties_keys:
114+
property_schema[key] = additional_properties
114115
property_schema.update(object_schema.get("properties", {}))
115116
return property_schema
116117

@@ -135,11 +136,7 @@ def _assign_dynamic_config_values(self, schema, config, parent_keys=None):
135136
for config_item_key, config_item_value in iterator:
136137
if config_is_dict:
137138
# different schema for each key/value pair
138-
try:
139-
# do not use schema.get() as schema might be a defaultdict
140-
schema_item = schema[config_item_key]
141-
except KeyError:
142-
schema_item = {}
139+
schema_item = schema.get(config_item_key, {})
143140
if config_is_list:
144141
# same schema is shared between every item in the list
145142
schema_item = schema
@@ -150,7 +147,10 @@ def _assign_dynamic_config_values(self, schema, config, parent_keys=None):
150147
# Inspect nested object properties
151148
if is_dictionary:
152149
parent_keys += [str(config_item_key)]
153-
property_schema = self._get_object_property_schema(schema_item)
150+
property_schema = self._get_object_property_schema(
151+
schema_item,
152+
additional_properties_keys=config_item_value.keys(),
153+
)
154154
self._assign_dynamic_config_values(
155155
schema=property_schema,
156156
config=config[config_item_key],
@@ -216,11 +216,7 @@ def _assign_default_values(self, schema, config):
216216

217217
property_schema = self._get_object_property_schema(
218218
schema_item,
219-
init_additional_properties=(
220-
config[schema_item_key].keys()
221-
if has_additional_properties
222-
else None
223-
),
219+
additional_properties_keys=config[schema_item_key].keys(),
224220
)
225221

226222
self._assign_default_values(

0 commit comments

Comments
 (0)