jatinmehra commited on
Commit
4fef33e
·
1 Parent(s): cde2965

Enhance OpenAPI schema customization for improved Swagger UI file upload support

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from contextlib import asynccontextmanager
4
  from API.core.config import settings
@@ -38,6 +39,7 @@ def create_app() -> FastAPI:
38
  version="1.0.0",
39
  lifespan=lifespan,
40
  docs_url="/",
 
41
  )
42
 
43
  # Setup standard CORS middleware to allow cross-origin requests
@@ -52,6 +54,33 @@ def create_app() -> FastAPI:
52
  # Include routers
53
  app.include_router(api_router, prefix="/api/v1")
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  return app
56
 
57
  app = create_app()
 
1
  from fastapi import FastAPI
2
+ from fastapi.openapi.utils import get_openapi
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from contextlib import asynccontextmanager
5
  from API.core.config import settings
 
39
  version="1.0.0",
40
  lifespan=lifespan,
41
  docs_url="/",
42
+ openapi_version="3.0.2",
43
  )
44
 
45
  # Setup standard CORS middleware to allow cross-origin requests
 
54
  # Include routers
55
  app.include_router(api_router, prefix="/api/v1")
56
 
57
+ def custom_openapi():
58
+ if app.openapi_schema:
59
+ return app.openapi_schema
60
+ openapi_schema = get_openapi(
61
+ title=app.title,
62
+ version=app.version,
63
+ description=app.description,
64
+ routes=app.routes,
65
+ )
66
+ # Fix for Swagger UI not showing file upload buttons in some configurations
67
+ try:
68
+ batch_files_prop = openapi_schema["components"]["schemas"]["Body_predict_batch_api_v1_predict_batch_post"]["properties"]["files"]
69
+ batch_files_prop["items"]["format"] = "binary"
70
+ except KeyError:
71
+ pass
72
+
73
+ try:
74
+ single_file_prop = openapi_schema["components"]["schemas"]["Body_predict_image_api_v1_predict_post"]["properties"]["file"]
75
+ single_file_prop["format"] = "binary"
76
+ except KeyError:
77
+ pass
78
+
79
+ app.openapi_schema = openapi_schema
80
+ return app.openapi_schema
81
+
82
+ app.openapi = custom_openapi
83
+
84
  return app
85
 
86
  app = create_app()