Buckets:

download
raw
18.5 kB
"""
OpenAPI 3.0 specification generator for Zenith applications.
Analyzes routes, type hints, and Pydantic models to automatically
generate comprehensive API documentation.
"""
import inspect
import types
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from enum import Enum
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Union,
get_args,
get_origin,
get_type_hints,
)
from uuid import UUID
if TYPE_CHECKING:
from zenith.core.routing import Router
from pydantic import BaseModel
from zenith.core.routing import AuthDependency, InjectDependency, RouteSpec
class OpenAPIGenerator:
"""
Generates OpenAPI 3.0 specifications from Zenith applications.
Features:
- Automatic route analysis
- Pydantic model schema extraction
- Request/response documentation
- Authentication documentation
- Parameter detection (path, query, body)
"""
def __init__(
self,
title: str = "Zenith API",
version: str = "1.0.0",
description: str = "API built with Zenith framework",
servers: list[dict[str, str]] | None = None,
):
self.title = title
self.version = version
self.description = description
self.servers = servers or [{"url": "/", "description": "Development server"}]
# Store schemas for reuse
self.schemas: dict[str, dict] = {}
self.components: dict[str, Any] = {"schemas": self.schemas}
# Simple in-memory cache for generated specs
_spec_cache: ClassVar[dict[str, dict]] = {}
def _get_cache_key(self, routers: list["Router"]) -> str:
"""Create simple string cache key from router structure."""
route_sigs = []
for router in routers:
for route_spec in router.routes:
# Include all spec fields that affect output
sig_parts = [
route_spec.path,
",".join(sorted(route_spec.methods)),
route_spec.handler.__name__,
str(route_spec.include_in_schema),
str(route_spec.status_code),
route_spec.summary or "",
route_spec.description or "",
route_spec.response_description,
",".join(route_spec.tags) if route_spec.tags else "",
route_spec.response_model.__name__
if route_spec.response_model
else "",
]
route_sigs.append(":".join(sig_parts))
routes_hash = hash(tuple(sorted(route_sigs)))
config_hash = hash((self.title, self.version, self.description))
return f"{routes_hash}_{config_hash}"
def generate_spec(self, routers: list["Router"]) -> dict[str, Any]:
"""Generate complete OpenAPI 3.0 specification with caching."""
# Check cache first for performance optimization
cache_key = self._get_cache_key(routers)
if cache_key in self._spec_cache:
return self._spec_cache[cache_key].copy() # Return copy to avoid mutation
spec = {
"openapi": "3.0.3",
"info": {
"title": self.title,
"version": self.version,
"description": self.description,
},
"servers": self.servers,
"paths": {},
"components": self.components,
}
# Process all routers
for router in routers:
for route_spec in router.routes:
self._process_route(spec, route_spec)
# Cache the result for future use (25-40% speedup for repeated calls)
self._spec_cache[cache_key] = spec.copy()
# Simple cache size management (LRU-like behavior)
if len(self._spec_cache) > 50: # Keep cache bounded
# Remove oldest entries
oldest_keys = list(self._spec_cache.keys())[:-25] # Keep newest 25
for key in oldest_keys:
del self._spec_cache[key]
return spec
def _process_route(self, spec: dict, route_spec: RouteSpec) -> None:
"""Process a single route and add to spec."""
# Skip routes excluded from schema
if not route_spec.include_in_schema:
return
path = route_spec.path
methods = route_spec.methods
handler = route_spec.handler
# Ensure path exists in spec
if path not in spec["paths"]:
spec["paths"][path] = {}
# Get handler signature and type hints
sig = inspect.signature(handler)
type_hints = get_type_hints(handler)
# Process each HTTP method
for method in methods:
method_lower = method.lower()
# Use RouteSpec fields with docstring fallback
summary = route_spec.summary or self._get_operation_summary(handler, method)
description = route_spec.description or self._get_operation_description(
handler
)
# Determine response type: RouteSpec.response_model > return type hint
response_type = route_spec.response_model or type_hints.get("return")
# Build operation spec
operation = {
"summary": summary,
"description": description,
"operationId": f"{method_lower}_{path.replace('/', '_').replace('{', '').replace('}', '')}",
"parameters": [],
"responses": self._get_responses(
response_type,
route_spec.status_code,
route_spec.response_description,
),
}
# Add tags if specified
if route_spec.tags:
operation["tags"] = route_spec.tags
# Process parameters
request_body = None
for param_name, param in sig.parameters.items():
param_type = type_hints.get(param_name, param.annotation)
# Skip special parameters
if param_name == "request":
continue
# Handle dependency injection markers
if isinstance(param.default, InjectDependency | AuthDependency):
if isinstance(param.default, AuthDependency):
# Add security requirement
if "security" not in operation:
operation["security"] = []
operation["security"].append({"bearerAuth": []})
continue
# Handle Pydantic models (request body)
from zenith.core.patterns import METHODS_WITH_BODY
if (
inspect.isclass(param_type)
and issubclass(param_type, BaseModel)
and method.upper() in METHODS_WITH_BODY
):
schema_name = param_type.__name__
self._add_schema(param_type)
request_body = {
"required": True,
"content": {
"application/json": {
"schema": {
"$ref": f"#/components/schemas/{schema_name}"
}
}
},
}
continue
# Handle path parameters
if param_name in self._extract_path_params(path):
operation["parameters"].append(
{
"name": param_name,
"in": "path",
"required": True,
"schema": self._get_type_schema(param_type),
"description": f"The {param_name} identifier",
}
)
continue
# Handle query parameters
is_required = param.default == inspect.Parameter.empty
operation["parameters"].append(
{
"name": param_name,
"in": "query",
"required": is_required,
"schema": self._get_type_schema(param_type),
"description": f"Query parameter: {param_name}",
}
)
# Add request body if present
if request_body:
operation["requestBody"] = request_body
# Add to spec
spec["paths"][path][method_lower] = operation
def _get_operation_summary(self, handler: callable, method: str) -> str:
"""Generate operation summary from handler."""
# Try to get from docstring first line
if handler.__doc__:
first_line = handler.__doc__.strip().split("\n")[0]
if first_line:
return first_line
# Generate from handler name and method
handler_name = handler.__name__.replace("_", " ").title()
return f"{method} {handler_name}"
def _get_operation_description(self, handler: callable) -> str:
"""Generate operation description from handler docstring."""
if handler.__doc__:
lines = handler.__doc__.strip().split("\n")
if len(lines) > 1:
# Return everything after the first line
return "\n".join(lines[1:]).strip()
return lines[0]
return f"Handler: {handler.__name__}"
def _extract_path_params(self, path: str) -> list[str]:
"""Extract parameter names from path template."""
from zenith.core.patterns import extract_path_params
return extract_path_params(path)
def _get_responses(
self,
return_type: type | None,
status_code: int = 200,
response_description: str = "Successful Response",
) -> dict[str, Any]:
"""Generate response specifications from return type."""
responses = {}
status_key = str(status_code)
if return_type and return_type != inspect.Parameter.empty:
# Handle Pydantic model response
if inspect.isclass(return_type) and issubclass(return_type, BaseModel):
schema_name = return_type.__name__
self._add_schema(return_type)
responses[status_key] = {
"description": response_description,
"content": {
"application/json": {
"schema": {"$ref": f"#/components/schemas/{schema_name}"}
}
},
}
elif (
return_type is dict
or self._is_dict_type(return_type)
or return_type is list
or self._is_list_type(return_type)
):
responses[status_key] = {
"description": response_description,
"content": {
"application/json": {
"schema": self._get_type_schema(return_type)
}
},
}
else:
responses[status_key] = {
"description": response_description,
"content": {
"application/json": {
"schema": self._get_type_schema(return_type)
}
},
}
else:
# Default response (no content for 204, etc.)
if status_code == 204:
responses[status_key] = {"description": response_description}
else:
responses[status_key] = {"description": response_description}
# Add common error responses
responses["400"] = {"description": "Bad Request"}
responses["401"] = {"description": "Unauthorized"}
responses["403"] = {"description": "Forbidden"}
responses["404"] = {"description": "Not Found"}
responses["422"] = {"description": "Validation Error"}
responses["429"] = {"description": "Rate Limited"}
responses["500"] = {"description": "Internal Server Error"}
return responses
def _is_dict_type(self, type_hint: type) -> bool:
"""Check if type hint represents a dict."""
origin = get_origin(type_hint)
return origin is dict
def _is_list_type(self, type_hint: type) -> bool:
"""Check if type hint represents a list."""
origin = get_origin(type_hint)
return origin is list
def _get_type_schema(self, type_hint: type) -> dict[str, Any]:
"""Convert Python type to OpenAPI schema."""
# Handle None type
if type_hint is None or type_hint is type(None):
return {"nullable": True}
# Basic types
if type_hint is str:
return {"type": "string"}
if type_hint is int:
return {"type": "integer"}
if type_hint is float:
return {"type": "number"}
if type_hint is bool:
return {"type": "boolean"}
if type_hint is bytes:
return {"type": "string", "format": "binary"}
# Date/time types
if type_hint is datetime:
return {"type": "string", "format": "date-time"}
if type_hint is date:
return {"type": "string", "format": "date"}
if type_hint is time:
return {"type": "string", "format": "time"}
if type_hint is timedelta:
return {"type": "string", "format": "duration"}
# UUID
if type_hint is UUID:
return {"type": "string", "format": "uuid"}
# Decimal
if type_hint is Decimal:
return {"type": "string", "format": "decimal"}
# Path
if type_hint is Path:
return {"type": "string", "format": "path"}
# Enum types
if inspect.isclass(type_hint) and issubclass(type_hint, Enum):
enum_values = [e.value for e in type_hint]
# Infer type from first value
if enum_values:
first_val = enum_values[0]
if isinstance(first_val, int):
return {"type": "integer", "enum": enum_values}
elif isinstance(first_val, str):
return {"type": "string", "enum": enum_values}
return {"enum": enum_values}
# Pydantic models
if inspect.isclass(type_hint) and issubclass(type_hint, BaseModel):
schema_name = type_hint.__name__
self._add_schema(type_hint)
return {"$ref": f"#/components/schemas/{schema_name}"}
# Generic types (List, Dict, Optional, Union)
origin = get_origin(type_hint)
args = get_args(type_hint)
# List[T]
if origin is list:
if args:
return {"type": "array", "items": self._get_type_schema(args[0])}
return {"type": "array", "items": {"type": "object"}}
# Dict[K, V]
if origin is dict:
if args and len(args) >= 2:
return {
"type": "object",
"additionalProperties": self._get_type_schema(args[1]),
}
return {"type": "object"}
# Optional[T] / T | None / Union[T, None]
# Handle both typing.Union and types.UnionType (Python 3.10+)
if origin is Union or isinstance(type_hint, types.UnionType):
non_none_args = [a for a in args if a is not type(None)]
if len(non_none_args) == 1:
# Optional[T] - single type + None
schema = self._get_type_schema(non_none_args[0])
schema["nullable"] = True
return schema
elif len(non_none_args) > 1:
# Union of multiple types - use oneOf
return {"oneOf": [self._get_type_schema(a) for a in non_none_args]}
# tuple types
if origin is tuple:
if args:
return {
"type": "array",
"items": [self._get_type_schema(a) for a in args],
"minItems": len(args),
"maxItems": len(args),
}
return {"type": "array"}
# set types
if origin is set or origin is frozenset:
if args:
return {
"type": "array",
"items": self._get_type_schema(args[0]),
"uniqueItems": True,
}
return {"type": "array", "uniqueItems": True}
# Plain list/dict without type params
if type_hint is list:
return {"type": "array", "items": {"type": "object"}}
if type_hint is dict:
return {"type": "object"}
# Fallback for unknown types
return {"type": "object"}
def _add_schema(self, model_class: type[BaseModel]) -> None:
"""Add Pydantic model schema to components."""
schema_name = model_class.__name__
if schema_name not in self.schemas:
# Get Pydantic schema
schema = model_class.model_json_schema()
# Remove $defs and move them to components
if "$defs" in schema:
for def_name, def_schema in schema["$defs"].items():
self.schemas[def_name] = def_schema
del schema["$defs"]
self.schemas[schema_name] = schema
def generate_openapi_spec(
routers: list["Router"],
title: str = "Zenith API",
version: str = "1.0.0",
description: str = "API built with Zenith framework",
servers: list[dict[str, str]] | None = None,
) -> dict[str, Any]:
"""
Generate OpenAPI specification from Zenith routers.
Args:
routers: List of Zenith routers to analyze
title: API title
version: API version
description: API description
servers: List of server configurations
Returns:
OpenAPI 3.0 specification as dictionary
"""
generator = OpenAPIGenerator(title, version, description, servers)
return generator.generate_spec(routers)

Xet Storage Details

Size:
18.5 kB
·
Xet hash:
a29693a0546079307766d9d30fd5a05f145af858eb6161e1adc105b8c4c75f69

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.