Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/fastapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.responses import HTMLResponse, StreamingResponse

from datastar_py.fastapi import (
DatastarResponse,
Expand Down Expand Up @@ -59,7 +59,7 @@
"""


@app.get("/")
@app.get("/", response_class=HTMLResponse)
async def read_root():
return HTMLResponse(HTML.replace("CURRENT_TIME", f"{datetime.isoformat(datetime.now())}"))

Expand All @@ -76,7 +76,7 @@ async def time_updates():
await asyncio.sleep(1)


@app.get("/updates")
@app.get("/updates", response_class=StreamingResponse)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it just be DatastarResponse? What does that do to the swagger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With response_class=StreamingResponse,
swagger only says there is no parameters and code 200 as Successful Response.
There is no media type documented at all (but previously it incorrectly indicated an application/json, so it's better that way).

So in this way, it's consistent.

With response_class=DatastarResponse,
swagger crash with Fetch error : Internal Server Error /openapi.json
The exception stack trace says:

...
        external_docs=self.openapi_external_docs,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/joris/.cache/uv/environments-v2/app-df56c0d93ab9210a/lib/python3.13/site-packages/fastapi/openapi/utils.py", line 520, in get_openapi
    result = get_openapi_path(
        route=route,
    ...<3 lines>...
        separate_input_output_schemas=separate_input_output_schemas,
    )
  File "/home/joris/.cache/uv/environments-v2/app-df56c0d93ab9210a/lib/python3.13/site-packages/fastapi/openapi/utils.py", line 352, in get_openapi_path
    operation.setdefault("responses", {}).setdefault(status_code, {})[
                                                     ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'status_code' where it is not associated with a value

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thanks for testing. Let's put it how you have for now. I'm curious if we could fix DatastarResponse to be usable as a response_class (for both swagger and automatically transforming the returns) and what that would look like. For the swagger purposes I still don't think it's that important, because a d* app would tend to be a non-api app.

async def updates(signals: ReadSignals):
# ReadSignals is a dependency that automatically loads the signals from the request
print(signals)
Expand Down
Loading