Refactor add_schema_to_components to move definitions to components/schemas and add corresponding unit test (#17389)

This commit is contained in:
Richard Song
2025-12-02 21:57:07 -08:00
committed by GitHub
parent 427074ac6e
commit 099ccf56a7
2 changed files with 30 additions and 1 deletions
@@ -72,7 +72,7 @@ class CustomOpenAPISpec:
openapi_schema["components"]["schemas"] = {}
# Add the schema
openapi_schema["components"]["schemas"][schema_name] = schema_def
CustomOpenAPISpec._move_defs_to_components(openapi_schema, {schema_name: schema_def})
@staticmethod
def add_request_body_to_paths(openapi_schema: Dict[str, Any], paths: List[str], schema_ref: str) -> None:
@@ -90,6 +90,35 @@ class TestCustomOpenAPISpec:
)
assert result == base_openapi_schema
def test_defs_rewritten_in_add_schema_to_components():
"""
Test that defs are rewritten to components/schemas in add_schema_to_components.
"""
openapi_schema = {}
schema_name = "SchemaName"
schema_def = {
"type": "object",
"properties": {
"messages": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/$defs/UserMessage"},
{"$ref": "#/$defs/AssistantMessage"}
]
}
}
},
"$defs": {
"UserMessage": {"type": "object"},
"AssistantMessage": {"type": "object"}
}
}
CustomOpenAPISpec.add_schema_to_components(openapi_schema=openapi_schema, schema_name=schema_name, schema_def=schema_def)
assert "$defs" not in openapi_schema
assert openapi_schema["components"]["schemas"]["SchemaName"]["properties"]["messages"]["items"]["anyOf"][0]["$ref"] == "#/components/schemas/UserMessage"
assert openapi_schema["components"]["schemas"]["SchemaName"]["properties"]["messages"]["items"]["anyOf"][1]["$ref"] == "#/components/schemas/AssistantMessage"
def test_move_defs_to_components():
"""