Skip to content

Commit 1b5d21a

Browse files
committed
Fix list access in config_loader._assign_default_values
It was trying to access lists as if they were dictionaries. So, adjust to allow both dicts and lists.
1 parent e679487 commit 1b5d21a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

st2common/st2common/util/config_loader.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,33 @@ def _assign_default_values(self, schema, config):
252252
253253
Note: This method mutates config argument in place.
254254
255-
:rtype: ``dict``
255+
:rtype: ``dict|list``
256256
"""
257257
schema_is_dict = isinstance(schema, dict)
258258
iterator = schema.items() if schema_is_dict else enumerate(schema)
259+
config_is_dict = isinstance(config, dict)
259260

261+
# _get_*_schema ensures that schema_item is always a dict
260262
for schema_item_key, schema_item in iterator:
261263
has_default_value = "default" in schema_item
262-
has_config_value = schema_item_key in config
264+
if config_is_dict:
265+
has_config_value = schema_item_key in config
266+
else:
267+
has_config_value = schema_item_key < len(config)
263268

264269
default_value = schema_item.get("default", None)
265270
if has_default_value and not has_config_value:
266271
# Config value is not provided, but default value is, use a default value
267272
config[schema_item_key] = default_value
268273

274+
if config_is_dict:
275+
config_value = config.get(schema_item_key, None)
276+
else:
277+
try:
278+
config_value = config[schema_item_key]
279+
except IndexError:
280+
config_value = None
281+
269282
schema_item_type = schema_item.get("type", None)
270283

271284
if schema_item_type == "object":
@@ -281,32 +294,32 @@ def _assign_default_values(self, schema, config):
281294
or has_pattern_properties
282295
or has_additional_properties
283296
):
284-
if not config.get(schema_item_key, None):
285-
config[schema_item_key] = {}
297+
if not config_value:
298+
config_value = config[schema_item_key] = {}
286299

287300
property_schema = self._get_object_property_schema(
288301
schema_item,
289-
additional_properties_keys=config[schema_item_key].keys(),
302+
additional_properties_keys=config_value.keys(),
290303
)
291304

292305
self._assign_default_values(
293-
schema=property_schema, config=config[schema_item_key]
306+
schema=property_schema, config=config_value
294307
)
295308
elif schema_item_type == "array":
296309
has_items = schema_item.get("items", None)
297310
has_additional_items = schema_item.get("additionalItems", None)
298311

299312
# Inspect nested array items
300313
if has_items or has_additional_items:
301-
if not config.get(schema_item_key, None):
302-
config[schema_item_key] = []
314+
if not config_value:
315+
config_value = config[schema_item_key] = []
303316

304317
items_schema = self._get_array_items_schema(
305318
schema_item,
306-
items_count=len(config[schema_item_key]),
319+
items_count=len(config_value),
307320
)
308321
self._assign_default_values(
309-
schema=items_schema, config=config[schema_item_key]
322+
schema=items_schema, config=config_value
310323
)
311324

312325
return config

0 commit comments

Comments
 (0)