|
7 | 7 | from urllib.parse import urlparse
|
8 | 8 |
|
9 | 9 | from fastapi import Cookie, Depends, FastAPI, Request, Response, status
|
| 10 | +from fastapi.openapi.utils import get_openapi |
10 | 11 | from fastapi.responses import FileResponse, RedirectResponse
|
11 | 12 | from fastapi.staticfiles import StaticFiles
|
12 | 13 | from fastapi.templating import Jinja2Templates
|
|
75 | 76 | templates = Jinja2Templates(directory="app/templates")
|
76 | 77 |
|
77 | 78 |
|
| 79 | +def custom_openapi(): |
| 80 | + if app.openapi_schema: |
| 81 | + return app.openapi_schema |
| 82 | + |
| 83 | + openapi_schema = get_openapi( |
| 84 | + title="OnboardLite API", |
| 85 | + version="1.0.0", |
| 86 | + description="Hack@UCF's in-house membership management suite", |
| 87 | + routes=app.routes, |
| 88 | + ) |
| 89 | + |
| 90 | + # Fix parameter type issues |
| 91 | + for path, path_item in openapi_schema.get("paths", {}).items(): |
| 92 | + for method, operation in path_item.items(): |
| 93 | + if method in ["get", "post", "put", "delete", "patch"]: |
| 94 | + # Fix operation IDs to ensure uniqueness |
| 95 | + if "operationId" in operation: |
| 96 | + operation_id = operation["operationId"] |
| 97 | + if operation_id.endswith("_post") and method == "get": |
| 98 | + operation["operationId"] = operation_id.replace("_post", "_get") |
| 99 | + |
| 100 | + # Fix parameter schemas |
| 101 | + if "parameters" in operation: |
| 102 | + for param in operation["parameters"]: |
| 103 | + if "schema" in param: |
| 104 | + schema = param["schema"] |
| 105 | + # Fix anyOf nullable patterns |
| 106 | + if "anyOf" in schema: |
| 107 | + any_of = schema["anyOf"] |
| 108 | + if len(any_of) == 2: |
| 109 | + string_type = None |
| 110 | + null_type = None |
| 111 | + for item in any_of: |
| 112 | + if item.get("type") == "string": |
| 113 | + string_type = item |
| 114 | + elif item.get("type") == "null": |
| 115 | + null_type = item |
| 116 | + |
| 117 | + if string_type and null_type: |
| 118 | + # Convert to nullable string |
| 119 | + param["schema"] = {"type": "string", "nullable": True, "title": schema.get("title", "")} |
| 120 | + if not param.get("required", True): |
| 121 | + param["required"] = False |
| 122 | + |
| 123 | + app.openapi_schema = openapi_schema |
| 124 | + return app.openapi_schema |
| 125 | + |
| 126 | + |
| 127 | +app.openapi = custom_openapi |
| 128 | + |
| 129 | + |
78 | 130 | def global_context(request: Request):
|
79 | 131 | return {
|
80 | 132 | "sentry_url": Settings().telemetry.url if Settings().telemetry.enable else None,
|
|
0 commit comments