Skip to content

Instantly share code, notes, and snippets.

@kokardy
Created October 10, 2023 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kokardy/ce2e76ebf979737c65df9c4ba0d5f87e to your computer and use it in GitHub Desktop.
Save kokardy/ce2e76ebf979737c65df9c4ba0d5f87e to your computer and use it in GitHub Desktop.
get openapi description
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
res = dict(message="validation error")
req_url = request.url
routes = app.routes
path = str(req_url).replace("http://testserver", "")
logger.debug(f"url: {req_url}")
logger.debug(f"path: {path}")
for route in routes:
logger.debug(f"route: {route} {route.path_regex} {route.methods}")
if route.path_regex.match(path) and request.method in route.methods:
logger.debug(f"********************route: {route}")
url = route.path
# path_spec = app.openapi()["paths"]["/pet/{id}"]
path_spec = app.openapi()["paths"][url]
req_body = path_spec["put"]["requestBody"]
model = req_body["content"]["application/json"]["schema"]["$ref"]
model_name = model.lstrip("#/components/schemas/")
from fastapi_test import models
Model = vars(models)[model_name]
name_field=Model.model_fields["name"]
logger.debug(f"name field: {name_field} {dir(name_field)}")
logger.debug(f"name desc: {name_field.description}")
return JSONResponse(res, status_code=400)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment