Repository navigation
🐛 Fix field aliases ignored for ServerSentEvent data in SSE streams - #16144
Closed
SAURABHSALVE wants to merge 1 commit into
Closed
SAURABHSALVE wants to merge 1 commit into
SAURABHSALVE wants to merge 1 commit into
Conversation
`ServerSentEvent(data=...)` serialized Pydantic models with `model_dump_json()`, whose `by_alias` default is `False`. Every other serialization path in FastAPI defaults to `by_alias=True`, so the same model produced different JSON depending on whether it was yielded directly or wrapped in a `ServerSentEvent`. Thread `response_model_by_alias` through both branches of the `ServerSentEvent` data serializer so it matches the plain-item path and honors an explicit `response_model_by_alias=False`.
Member
|
Thanks for your interest and efforts, but let's keep it closed until maintainers can review the discussion #15703 and decide how to handle it. We are trying to keep things clean and ask to only open PRs when it's explicitly requested by maintainers.
Thanks for understanding! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ServerSentEvent(data=...)serializes Pydantic models viamodel_dump_json(),whose
by_aliasdefault isFalse. Every other serialization path in FastAPIdefaults to
by_alias=True:jsonable_encoder()—by_alias: bool = TrueModelField.serialize_json()—by_alias: bool = Trueresponse_model_by_aliason all route decorators —TrueThis makes
ServerSentEventthe only path that drops aliases, so the same modelserializes differently depending on how it is yielded: