Skip to content

Instantly share code, notes, and snippets.

@dblock
Created July 16, 2024 19:07
Show Gist options
  • Save dblock/c783e208abe1cf3f989f29cb52e71558 to your computer and use it in GitHub Desktop.
Save dblock/c783e208abe1cf3f989f29cb52e71558 to your computer and use it in GitHub Desktop.
Expabnd Refs
def expand_refs(element: Any, data: Any) -> Any:
"""
Recursively expand $ref's.
"""
if isinstance(element, dict):
if "$ref" in element:
common_schema_path_ref = element["$ref"].split("/")[-1]
schema = data["components"]["schemas"][common_schema_path_ref]
del element["$ref"]
element = element | expand_refs(schema, data)
for key in element:
element[key] = expand_refs(element[key], data)
return element
if isinstance(element, list):
result = []
for item in element:
result.append(expand_refs(item, data))
return result
return element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment