Skip to content

Commit ef1a165

Browse files
committed
Cleanup openapi.json
1 parent 5674c64 commit ef1a165

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

app/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from urllib.parse import urlparse
88

99
from fastapi import Cookie, Depends, FastAPI, Request, Response, status
10+
from fastapi.openapi.utils import get_openapi
1011
from fastapi.responses import FileResponse, RedirectResponse
1112
from fastapi.staticfiles import StaticFiles
1213
from fastapi.templating import Jinja2Templates
@@ -75,6 +76,57 @@
7576
templates = Jinja2Templates(directory="app/templates")
7677

7778

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+
78130
def global_context(request: Request):
79131
return {
80132
"sentry_url": Settings().telemetry.url if Settings().telemetry.enable else None,

app/routes/stripe.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,50 @@ async def get_root(
5656
)
5757

5858

59-
@router.api_route("/checkout", methods=["GET", "POST"])
59+
@router.get("/checkout")
60+
async def get_checkout_session(
61+
request: Request,
62+
current_user: CurrentMember,
63+
session: Session = Depends(get_session),
64+
):
65+
"""Get checkout session information or redirect to existing session."""
66+
if Settings().stripe.pause_payments:
67+
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Payments are currently paused")
68+
69+
user_data = session.exec(select(UserModel).where(UserModel.id == uuid.UUID(current_user.get("id")))).one_or_none()
70+
if not user_data.email:
71+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No email associated with account")
72+
user_id = user_data.id
73+
try:
74+
stripe_email = user_data.email
75+
checkout_session = stripe.checkout.Session.create(
76+
line_items=[
77+
{
78+
# Provide the exact Price ID (for example, pr_1234) of the product you want to sell
79+
"price": Settings().stripe.price_id,
80+
"quantity": 1,
81+
},
82+
],
83+
customer_email=stripe_email,
84+
mode="payment",
85+
success_url=Settings().stripe.url_success,
86+
cancel_url=Settings().stripe.url_failure,
87+
metadata={"user_id": str(user_id)},
88+
)
89+
except Exception as e:
90+
logger.exception("Error creating checkout session in stripe.py", e)
91+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Error creating checkout session")
92+
93+
return RedirectResponse(checkout_session.url, status_code=303)
94+
95+
96+
@router.post("/checkout")
6097
async def create_checkout_session(
6198
request: Request,
6299
current_user: CurrentMember,
63100
session: Session = Depends(get_session),
64101
):
102+
"""Create a new Stripe checkout session."""
65103
if Settings().stripe.pause_payments:
66104
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Payments are currently paused")
67105

0 commit comments

Comments
 (0)