fastapi example: fix response_class#31
Conversation
|
|
||
|
|
||
| @app.get("/updates") | ||
| @app.get("/updates", response_class=StreamingResponse) |
There was a problem hiding this comment.
Should it just be DatastarResponse? What does that do to the swagger?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
Thanks! |
See issue #30
I've checked the correct response type for SSE: it's
"Content-Type: text/event-stream". ThusStreamingResponse