From 0f49782b178595a781c4c115c6e106b04829491f Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Sat, 23 Jul 2022 12:11:02 -0500 Subject: [PATCH] copy dependencies schema handling into assign_subschema_defaults --- st2common/st2common/util/schema/__init__.py | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/st2common/st2common/util/schema/__init__.py b/st2common/st2common/util/schema/__init__.py index 4db7c65ba7..14836ef7c7 100644 --- a/st2common/st2common/util/schema/__init__.py +++ b/st2common/st2common/util/schema/__init__.py @@ -587,7 +587,7 @@ def get_flattened_array_items_schema(array_schema, items_count=0): return flattened_items_schema -def assign_subschema_default_values(subschema, instance): +def assign_subschema_default_values(subschema, instance, dependencies=None): """ Assign default values for particular config if default values are provided in the config schema and a value is not specified in the config. @@ -603,6 +603,21 @@ def assign_subschema_default_values(subschema, instance): for schema_item_key, schema_item in iterator: has_default_value = "default" in schema_item + if has_default_value and dependencies and schema_item_key in dependencies: + # items are properties of an object. + # only populate default if dependencies are met + # eg: exclusiveMaximum depends on maximum which does not have a default. + # so we don't want to apply exclusiveMaximum's default unless maximum. + for required_property in dependencies[schema_item_key]: + if "default" in subschema.get(required_property, {}): + # we depend on something that has a default. Apply this default. + continue + if required_property not in instance: + # we depend on something that does not have a default. + # do not apply this default. + has_default_value = False + break + if isinstance(instance, dict): has_instance_value = schema_item_key in instance else: @@ -619,7 +634,7 @@ def assign_subschema_default_values(subschema, instance): instance_value = _assign_default_values( schema=schema_item, instance=instance_value, - can_replace_instance=not has_instance_value, + can_replace_instance=has_default_value and not has_instance_value, ) if ( @@ -657,7 +672,9 @@ def _assign_default_values(schema, instance, can_replace_instance=False): ) assign_subschema_default_values( - subschema=properties_schema, instance=instance + subschema=properties_schema, + instance=instance, + dependencies=schema.get("dependencies", {}), # deps between properties ) elif schema_type == "array":