Spaces:
Running
Running
ShiWei commited on
Commit ·
73c6a92
1
Parent(s): fe73cad
add tests for _replace_ref_with_defs
Browse files
src/fastmcp/utilities/openapi.py
CHANGED
|
@@ -894,13 +894,18 @@ def _replace_ref_with_defs(
|
|
| 894 |
dict[str, Any]
|
| 895 |
"""
|
| 896 |
schema = info.copy()
|
| 897 |
-
if
|
| 898 |
-
for prop_name, prop_schema in properties.items():
|
| 899 |
-
properties[prop_name] = _replace_ref_with_defs(prop_schema)
|
| 900 |
-
elif ref_path := schema.get("$ref"):
|
| 901 |
if ref_path.startswith("#/components/schemas/"):
|
| 902 |
schema_name = ref_path.split("/")[-1]
|
| 903 |
schema["$ref"] = f"#/$defs/{schema_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 904 |
elif item_schema := schema.get("items"):
|
| 905 |
schema["items"] = _replace_ref_with_defs(item_schema)
|
| 906 |
for section in ["anyOf", "allOf", "oneOf"]:
|
|
|
|
| 894 |
dict[str, Any]
|
| 895 |
"""
|
| 896 |
schema = info.copy()
|
| 897 |
+
if ref_path := schema.get("$ref"):
|
|
|
|
|
|
|
|
|
|
| 898 |
if ref_path.startswith("#/components/schemas/"):
|
| 899 |
schema_name = ref_path.split("/")[-1]
|
| 900 |
schema["$ref"] = f"#/$defs/{schema_name}"
|
| 901 |
+
elif properties := schema.get("properties"):
|
| 902 |
+
if "$ref" in properties:
|
| 903 |
+
schema["properties"] = _replace_ref_with_defs(properties)
|
| 904 |
+
else:
|
| 905 |
+
schema["properties"] = {
|
| 906 |
+
prop_name: _replace_ref_with_defs(prop_schema)
|
| 907 |
+
for prop_name, prop_schema in properties.items()
|
| 908 |
+
}
|
| 909 |
elif item_schema := schema.get("items"):
|
| 910 |
schema["items"] = _replace_ref_with_defs(item_schema)
|
| 911 |
for section in ["anyOf", "allOf", "oneOf"]:
|
tests/utilities/openapi/test_openapi.py
CHANGED
|
@@ -8,6 +8,7 @@ from pydantic import BaseModel, Field
|
|
| 8 |
|
| 9 |
from fastmcp.utilities.openapi import (
|
| 10 |
_combine_schemas,
|
|
|
|
| 11 |
parse_openapi_to_http_routes,
|
| 12 |
)
|
| 13 |
|
|
@@ -1102,3 +1103,67 @@ def test_consistent_output_across_versions(
|
|
| 1102 |
"properties"
|
| 1103 |
]
|
| 1104 |
assert set(schema_30.keys()) == set(schema_31.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from fastmcp.utilities.openapi import (
|
| 10 |
_combine_schemas,
|
| 11 |
+
_replace_ref_with_defs,
|
| 12 |
parse_openapi_to_http_routes,
|
| 13 |
)
|
| 14 |
|
|
|
|
| 1103 |
"properties"
|
| 1104 |
]
|
| 1105 |
assert set(schema_30.keys()) == set(schema_31.keys())
|
| 1106 |
+
|
| 1107 |
+
|
| 1108 |
+
def test_replace_ref_with_defs():
|
| 1109 |
+
ref_type = {
|
| 1110 |
+
"$ref": "#/components/schemas/RefFoo",
|
| 1111 |
+
}
|
| 1112 |
+
object_type = {
|
| 1113 |
+
"type": "object",
|
| 1114 |
+
"properties": {"$ref": "#/components/schemas/ObjectFoo"},
|
| 1115 |
+
}
|
| 1116 |
+
array_type = {"type": "array", "items": {"$ref": "#/components/schemas/ArrayFoo"}}
|
| 1117 |
+
any_of_type = {
|
| 1118 |
+
"anyOf": [
|
| 1119 |
+
{"$ref": "#/components/schemas/AnyOfFoo"},
|
| 1120 |
+
{"$ref": "#/components/schemas/AnyOfBar"},
|
| 1121 |
+
]
|
| 1122 |
+
}
|
| 1123 |
+
all_of_type = {
|
| 1124 |
+
"allOf": [
|
| 1125 |
+
{"$ref": "#/components/schemas/AllOfFoo"},
|
| 1126 |
+
{"$ref": "#/components/schemas/AllOfBar"},
|
| 1127 |
+
]
|
| 1128 |
+
}
|
| 1129 |
+
one_of_type = {
|
| 1130 |
+
"oneOf": [
|
| 1131 |
+
{"$ref": "#/components/schemas/OneOfFoo"},
|
| 1132 |
+
{"$ref": "#/components/schemas/OneOfBar"},
|
| 1133 |
+
]
|
| 1134 |
+
}
|
| 1135 |
+
nested_type = {
|
| 1136 |
+
"type": "object",
|
| 1137 |
+
"properties": {
|
| 1138 |
+
"pets": {
|
| 1139 |
+
"oneOf": [
|
| 1140 |
+
{"$ref": "#/components/schemas/Cat"},
|
| 1141 |
+
{"$ref": "#/components/schemas/Dog"},
|
| 1142 |
+
]
|
| 1143 |
+
},
|
| 1144 |
+
},
|
| 1145 |
+
}
|
| 1146 |
+
assert _replace_ref_with_defs(object_type) == {
|
| 1147 |
+
"type": "object",
|
| 1148 |
+
"properties": {"$ref": "#/$defs/ObjectFoo"},
|
| 1149 |
+
}
|
| 1150 |
+
assert _replace_ref_with_defs(ref_type) == {"$ref": "#/$defs/RefFoo"}
|
| 1151 |
+
assert _replace_ref_with_defs(array_type) == {
|
| 1152 |
+
"type": "array",
|
| 1153 |
+
"items": {"$ref": "#/$defs/ArrayFoo"},
|
| 1154 |
+
}
|
| 1155 |
+
assert _replace_ref_with_defs(any_of_type) == {
|
| 1156 |
+
"anyOf": [{"$ref": "#/$defs/AnyOfFoo"}, {"$ref": "#/$defs/AnyOfBar"}]
|
| 1157 |
+
}
|
| 1158 |
+
assert _replace_ref_with_defs(all_of_type) == {
|
| 1159 |
+
"allOf": [{"$ref": "#/$defs/AllOfFoo"}, {"$ref": "#/$defs/AllOfBar"}]
|
| 1160 |
+
}
|
| 1161 |
+
assert _replace_ref_with_defs(one_of_type) == {
|
| 1162 |
+
"oneOf": [{"$ref": "#/$defs/OneOfFoo"}, {"$ref": "#/$defs/OneOfBar"}]
|
| 1163 |
+
}
|
| 1164 |
+
assert _replace_ref_with_defs(nested_type) == {
|
| 1165 |
+
"type": "object",
|
| 1166 |
+
"properties": {
|
| 1167 |
+
"pets": {"oneOf": [{"$ref": "#/$defs/Cat"}, {"$ref": "#/$defs/Dog"}]}
|
| 1168 |
+
},
|
| 1169 |
+
}
|