Skip to content

Commit

Permalink
copy dependencies schema handling into assign_subschema_defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
cognifloyd committed Jul 23, 2022
1 parent d24df5b commit 0f49782
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions st2common/st2common/util/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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 (
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 0f49782

Please sign in to comment.