fastapi/fastapi
Charisn 05d1effc23 Fix UnboundLocalError in OpenAPI schema generation with custom response classes
When a route uses a custom response class whose __init__ does not expose
a status_code parameter with an int default, and the route decorator
does not specify an explicit status_code, the status_code local variable
in get_openapi_path() was never assigned. This caused an
UnboundLocalError at the point where status_code is used to populate
the operation responses dict.

The bug does not surface with standard Starlette response classes
(JSONResponse, HTMLResponse, PlainTextResponse, StreamingResponse, etc.)
because they all declare status_code: int = 200 (or 307 for redirects)
in their __init__ signatures. It triggers when users define custom
response classes that wrap or omit the status_code parameter, e.g.:

    class CustomResponse(Response):
        def __init__(self, content, **kwargs):
            super().__init__(content=content, status_code=200, **kwargs)

    @app.get("/items", response_class=CustomResponse)
    async def get_items(): ...

Accessing /openapi.json or /docs would crash with:
    UnboundLocalError: cannot access local variable 'status_code'

Fix: initialize status_code = "200" before the conditional block so it
always has a safe default. The existing branches that extract status_code
from the route or from the response class signature still override this
default when applicable.
2026-02-15 13:09:14 +02:00
..
_compat 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
dependencies 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
middleware 📝 Use `WSGIMiddleware` from `a2wsgi` instead of deprecated `fastapi.middleware.wsgi.WSGIMiddleware` (#14756) 2026-02-04 12:54:23 +01:00
openapi Fix UnboundLocalError in OpenAPI schema generation with custom response classes 2026-02-15 13:09:14 +02:00
security 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
__init__.py 🔖 Release version 0.129.0 2026-02-12 14:52:27 +01:00
__main__.py ♻️ Add support for `pip install "fastapi[standard]"` with standard dependencies and `python -m fastapi` (#11935) 2024-08-02 01:03:05 -05:00
applications.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
background.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
cli.py ♻️ Add support for `pip install "fastapi[standard]"` with standard dependencies and `python -m fastapi` (#11935) 2024-08-02 01:03:05 -05:00
concurrency.py ♻️ Upgrade internal syntax to Python 3.9+ 🎉 (#14564) 2025-12-17 21:25:59 +00:00
datastructures.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
encoders.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
exception_handlers.py ⬆️ Upgrade Starlette supported version range to >=0.40.0,<0.49.0 (#14077) 2025-09-16 19:21:48 +02:00
exceptions.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
logger.py 🔊 Refactor logging (#781) 2019-12-09 20:02:44 +01:00
param_functions.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
params.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
py.typed add py.typed to ship typing information (#209) 2019-05-11 13:43:47 +04:00
requests.py ⬆ Require Pydantic > 1.0 (#1862) 2020-08-09 22:17:08 +02:00
responses.py Add reference (code API) docs with PEP 727, add subclass with custom docstrings for `BackgroundTasks`, refactor docs structure (#10392) 2023-10-18 16:36:40 +04:00
routing.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
staticfiles.py Improve type annotations, add support for mypy --strict, internally and for external packages (#2547) 2020-12-20 19:50:00 +01:00
templating.py Improve type annotations, add support for mypy --strict, internally and for external packages (#2547) 2020-12-20 19:50:00 +01:00
testclient.py Improve type annotations, add support for mypy --strict, internally and for external packages (#2547) 2020-12-20 19:50:00 +01:00
types.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
utils.py 🎨 Update internal types for Python 3.10 (#14898) 2026-02-11 18:41:21 +00:00
websockets.py Export `WebSocketState` in `fastapi.websockets` (#4376) 2022-09-04 17:12:10 +02:00