Spaces:
Running
Running
Jeremiah Lowin Claude commited on
fix: handle non-string $ref values in experimental OpenAPI parser (#1217)
Browse files
src/fastmcp/experimental/utilities/openapi/parser.py
CHANGED
|
@@ -146,6 +146,9 @@ class OpenAPIParser(
|
|
| 146 |
"""Resolves a reference to its target definition."""
|
| 147 |
if isinstance(item, self.reference_cls):
|
| 148 |
ref_str = item.ref
|
|
|
|
|
|
|
|
|
|
| 149 |
try:
|
| 150 |
if not ref_str.startswith("#/"):
|
| 151 |
raise ValueError(
|
|
|
|
| 146 |
"""Resolves a reference to its target definition."""
|
| 147 |
if isinstance(item, self.reference_cls):
|
| 148 |
ref_str = item.ref
|
| 149 |
+
# Ensure ref_str is a string before calling startswith()
|
| 150 |
+
if not isinstance(ref_str, str):
|
| 151 |
+
return item
|
| 152 |
try:
|
| 153 |
if not ref_str.startswith("#/"):
|
| 154 |
raise ValueError(
|
src/fastmcp/experimental/utilities/openapi/schemas.py
CHANGED
|
@@ -93,15 +93,16 @@ def _replace_ref_with_defs(
|
|
| 93 |
"""
|
| 94 |
schema = info.copy()
|
| 95 |
if ref_path := schema.get("$ref"):
|
| 96 |
-
if
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
elif properties := schema.get("properties"):
|
| 106 |
if "$ref" in properties:
|
| 107 |
schema["properties"] = _replace_ref_with_defs(properties)
|
|
|
|
| 93 |
"""
|
| 94 |
schema = info.copy()
|
| 95 |
if ref_path := schema.get("$ref"):
|
| 96 |
+
if isinstance(ref_path, str):
|
| 97 |
+
if ref_path.startswith("#/components/schemas/"):
|
| 98 |
+
schema_name = ref_path.split("/")[-1]
|
| 99 |
+
schema["$ref"] = f"#/$defs/{schema_name}"
|
| 100 |
+
elif not ref_path.startswith("#/"):
|
| 101 |
+
raise ValueError(
|
| 102 |
+
f"External or non-local reference not supported: {ref_path}. "
|
| 103 |
+
f"FastMCP only supports local schema references starting with '#/'. "
|
| 104 |
+
f"Please include all schema definitions within the OpenAPI document."
|
| 105 |
+
)
|
| 106 |
elif properties := schema.get("properties"):
|
| 107 |
if "$ref" in properties:
|
| 108 |
schema["properties"] = _replace_ref_with_defs(properties)
|