Kludex / fastapi-responses

Find HTTPExceptions and turn them into documented responses! :tada:
https://github.com/Kludex/fastapi-responses
MIT License
92 stars 11 forks source link

Using `sqlalchemy.exists` inside path breaks the project with OSError #12

Open sashkent3 opened 2 years ago

sashkent3 commented 2 years ago

The problem is reproducible with:

from fastapi_responses import custom_openapi
from fastapi import FastAPI, HTTPException
from sqlalchemy import exists

app = FastAPI()
app.openapi = custom_openapi(app)

@app.get("/{item_id}")
def get_item(item_id: int):
    exists
    if item_id == 0:
        raise HTTPException(status_code=404, detail="Item not found.")
    return "Item exists!"

Full traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/fastapi/applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/usr/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/usr/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 75, in __call__
    raise exc
  File "/usr/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 64, in __call__
    await self.app(scope, receive, sender)
  File "/usr/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/starlette/routing.py", line 680, in __call__
    await route.handle(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/starlette/routing.py", line 275, in handle
    await self.app(scope, receive, send)
  File "/usr/lib/python3.10/site-packages/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/usr/lib/python3.10/site-packages/fastapi/applications.py", line 224, in openapi
    return JSONResponse(self.openapi())
  File "/home/sasha/.local/lib/python3.10/site-packages/fastapi_responses/openapi.py", line 21, in _custom_openapi
    for exception in extract_exceptions(route):
  File "/home/sasha/.local/lib/python3.10/site-packages/fastapi_responses/utils.py", line 56, in extract_exceptions
    source = inspect.getsource(endpoint)
  File "/usr/lib/python3.10/inspect.py", line 1147, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python3.10/inspect.py", line 1129, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.10/inspect.py", line 958, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

This StackOverflow post I've made while digging into the problem describes the route cause of it.