Browse Source

♻️ Refactor ServerSentEvent to use id: str

pull/15030/head
Sebastián Ramírez 5 months ago
parent
commit
d707105053
  1. 8
      fastapi/sse.py

8
fastapi/sse.py

@ -33,8 +33,8 @@ class EventSourceResponse(StreamingResponse):
media_type = "text/event-stream" media_type = "text/event-stream"
def _check_id_no_null(v: int | str | None) -> int | str | None: def _check_id_no_null(v: str | None) -> str | None:
if v is not None and "\0" in str(v): if v is not None and "\0" in v:
raise ValueError("SSE 'id' must not contain null characters") raise ValueError("SSE 'id' must not contain null characters")
return v return v
@ -96,7 +96,7 @@ class ServerSentEvent(BaseModel):
), ),
] = None ] = None
id: Annotated[ id: Annotated[
int | str | None, str | None,
AfterValidator(_check_id_no_null), AfterValidator(_check_id_no_null),
Doc( Doc(
""" """
@ -162,7 +162,7 @@ def format_sse_event(
), ),
] = None, ] = None,
id: Annotated[ id: Annotated[
int | str | None, str | None,
Doc( Doc(
""" """
Optional event ID (`id:` field). Optional event ID (`id:` field).

Loading…
Cancel
Save