Add charset=utf-8 to JSON responses on the orjson (bytes) path#696
Open
wbarnha wants to merge 1 commit into
Open
Add charset=utf-8 to JSON responses on the orjson (bytes) path#696wbarnha wants to merge 1 commit into
wbarnha wants to merge 1 commit into
Conversation
Web.json() serializes with faust.utils.json.dumps, which returns str with stdlib json but bytes with orjson. The str path goes through Response(text=...), which makes aiohttp append "; charset=utf-8" to the content-type; the bytes path went through Response(body=...), which does not -- so with orjson installed every JSON response was served as "application/json" with no charset, while the same app on stdlib json served "application/json; charset=utf-8". Build the bytes-path Response directly with an explicit charset="utf-8" so JSON responses are consistent regardless of the installed JSON backend. The generic bytes() helper is left unchanged (binary responses should not be forced to utf-8). Fixes #557. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HHPL4VFWQRQPpjR1gXSKyL
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #696 +/- ##
=======================================
Coverage 94.14% 94.15%
=======================================
Files 104 104
Lines 11136 11137 +1
Branches 1201 1201
=======================================
+ Hits 10484 10486 +2
+ Misses 551 550 -1
Partials 101 101 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
github-merge-queue
Bot
removed this pull request from the merge queue due to no response for status checks
Jul 21, 2026
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.
Note: Before submitting this pull request, please review our contributing guidelines.
Description
Fixes #557.
Web.json()serializes withfaust.utils.json.dumps, which returnsstrwith stdlibjsonbutbyteswithorjson. The two paths produced different content-types:Response(text=...)→ aiohttp appends; charset=utf-8.Response(body=...)→ aiohttp does not add a charset.So with
orjsoninstalled, every JSON response was served asapplication/json(no charset), while the same app on stdlibjsonservedapplication/json; charset=utf-8. This explains the reporter-vs-maintainer "can't reproduce" — it depends on whetherorjsonis installed.Fix
Build the bytes-path
Responsedirectly with an explicitcharset="utf-8"(verified:Response(body=..., content_type="application/json", charset="utf-8")→application/json; charset=utf-8). The genericbytes()helper is left unchanged — binary responses shouldn't be forced to utf-8.Tests
test_json__from_bytesfor the new implementation (buildsResponse(..., charset="utf-8")).test_json_content_type_has_charset_for_both_backends: asserts the realContent-Typeheader isapplication/json; charset=utf-8whetherdumpsreturnsbytes(orjson) orstr(stdlib).application/json(no charset).flake8/black --check/isort --check-onlyclean.🤖 Generated with Claude Code
Generated by Claude Code