This commit is contained in:
Tim Jaeryang Baek 2025-06-09 01:54:02 +04:00 committed by GitHub
commit 3eddb1558c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 0 deletions

View File

View File

@ -310,3 +310,18 @@ def test_multi_type_property_with_any_of():
# assert result_field parameter config
assert result_field.description == "A property with multiple types"
def test_ref_to_parent_node():
schema = {'$ref': '#/properties/data/properties/children/items'}
result_type, result_field = _process_schema_property(
_model_cache,
schema,
"generate_fishbone_diagram_form_model_data_model_children_item_model_children",
"item",
False,
{}
)
assert result_type == Any
assert result_field.description == ""

View File

@ -93,6 +93,19 @@ def _process_schema_property(
"""
if "$ref" in prop_schema:
ref = prop_schema["$ref"]
if ref.startswith("#/properties/"):
# Remove common prefix in pathes.
prefix_path = model_name_prefix.split("_form_model_")[-1]
ref_path = ref.split("#/properties/")[-1]
# Translate $ref path to model_name_prefix style.
ref_path = ref_path.replace("/properties/", "_model_")
ref_path = ref_path.replace("/items", "_item")
# If $ref path is a prefix substring of model_name_prefix path,
# there exists a circular reference.
# The loop should be broke with a return to avoid exception.
if prefix_path.startswith(ref_path):
# TODO: Find the exact type hint for the $ref.
return Any, Field(default=None, description="")
ref = ref.split("/")[-1]
assert ref in schema_defs, "Custom field not found"
prop_schema = schema_defs[ref]