Add OpenAPI extensions support to HTTPRoute

- Add extensions field to HTTPRoute class to store x-* fields
  - Extract extensions from operation's model_extra in parser
  - Add test to verify extensions are properly parsed
This commit is contained in:
Aditya Bansal 2025-06-27 15:26:55 -07:00
commit f524652f25
2 changed files with 33 additions and 0 deletions

View file

@ -84,6 +84,7 @@ class HTTPRoute(FastMCPBaseModel):
schema_definitions: dict[str, JsonSchema] = Field(
default_factory=dict
) # Store component schemas
extensions: dict[str, Any] = Field(default_factory=dict)
# Export public symbols
@ -591,6 +592,14 @@ class OpenAPIParser(
getattr(operation, "responses", None)
)
extensions = {}
if hasattr(operation, "model_extra") and operation.model_extra:
extensions = {
k: v
for k, v in operation.model_extra.items()
if k.startswith("x-")
}
route = HTTPRoute(
path=path_str,
method=method_upper, # type: ignore[arg-type] # Known valid HTTP method
@ -602,6 +611,7 @@ class OpenAPIParser(
request_body=request_body_info,
responses=responses,
schema_definitions=schema_definitions,
extensions=extensions,
)
routes.append(route)
logger.info(

View file

@ -687,6 +687,29 @@ def test_multiple_tags_preserved(bookstore_schema):
assert len(get_books.tags) == 3
def test_openapi_extensions(petstore_schema):
"""Test that OpenAPI extensions (x-*) are correctly parsed from operations."""
# Add extensions to a route
petstore_schema["paths"]["/pets"]["get"]["x-rate-limit"] = 100
petstore_schema["paths"]["/pets"]["get"]["x-custom-auth"] = "bearer"
petstore_schema["paths"]["/pets"]["get"]["x-internal"] = True
# Parse the modified schema
routes = parse_openapi_to_http_routes(petstore_schema)
# Find the GET /pets route
get_pets = next(
(r for r in routes if r.method == "GET" and r.path == "/pets"), None
)
assert get_pets is not None
# Should have extensions
assert get_pets.extensions["x-rate-limit"] == 100
assert get_pets.extensions["x-custom-auth"] == "bearer"
assert get_pets.extensions["x-internal"] is True
assert len(get_pets.extensions) == 3
# --- Tests for BookStore schema --- #