Skip to content

Commit

Permalink
fix swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
xgui3783 committed Apr 18, 2024
1 parent 173250f commit ef4e0c3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion api/common/data_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

if ROLE == "all" or ROLE == "worker":
import siibra
siibra.warm_cache()
siibra.warm_cache(siibra.WarmupLevel.INSTANCE)

2 changes: 1 addition & 1 deletion api/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
siibra_api.include_router(vocabolaries_router, prefix="/vocabularies")

add_pagination(siibra_api)
add_sample_code(siibra_api)

# Versioning for api endpoints
siibra_api = VersionedFastAPI(siibra_api)

add_sample_code(siibra_api)

templates = Jinja2Templates(directory="templates/")
siibra_api.mount("/static", StaticFiles(directory="static"), name="static")
Expand Down
73 changes: 34 additions & 39 deletions api/server/code_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,40 @@ def get_sourcecode(request: Request, lang: str="python") -> str:
raise NotFound

def add_sample_code(rootapp: FastAPI):
app = rootapp

for route in rootapp.routes:
if (isinstance(route, Mount)
and route.path == "/v3_0"
and isinstance(route.app, FastAPI)
):
app = route.app
def custom_api():
if app.openapi_schema:
return app.openapi_schema

openapi_schema = get_openapi(
title="siibra-api",
version="v3",
summary="siibra-api openapi specification",
description="siibra-api is a http wrapper around siibra-python",
routes=app.routes,
)

id_to_route = {
route.operation_id or route.unique_id: route
for route in app.routes
if isinstance(route, APIRoute)
}
def custom_api():
if app.openapi_schema:
return app.openapi_schema

openapi_schema = get_openapi(
title="siibra-api",
version="v3",
summary="siibra-api openapi specification",
description="siibra-api is a http wrapper around siibra-python",
routes=app.routes,
)

id_to_route = {
route.operation_id or route.unique_id: route
for route in app.routes
if isinstance(route, APIRoute)
}

for path_value in openapi_schema.get("paths").values():
for method_value in path_value.values():
op_id = method_value.get("operationId")
if op_id not in id_to_route:
continue
route = id_to_route[op_id]
if route.name not in name_to_fns_map:
continue
fn0, fn1 = name_to_fns_map[route.name]
method_value["x-codeSamples"] = [{
"lang": lang,
"source": code,
} for lang, code in yield_codeblock(fn0.__doc__)]
for path_value in openapi_schema.get("paths").values():
for method_value in path_value.values():
op_id = method_value.get("operationId")
if op_id not in id_to_route:
continue
route = id_to_route[op_id]
if route.name not in name_to_fns_map:
continue
fn0, fn1 = name_to_fns_map[route.name]
method_value["x-codeSamples"] = [{
"lang": lang,
"source": code,
} for lang, code in yield_codeblock(fn0.__doc__)]

app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_api
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_api

0 comments on commit ef4e0c3

Please sign in to comment.