Skip to content

Add charset=utf-8 to JSON responses on the orjson (bytes) path#696

Open
wbarnha wants to merge 1 commit into
masterfrom
claude/fix-557-json-charset
Open

Add charset=utf-8 to JSON responses on the orjson (bytes) path#696
wbarnha wants to merge 1 commit into
masterfrom
claude/fix-557-json-charset

Conversation

@wbarnha

@wbarnha wbarnha commented Jul 19, 2026

Copy link
Copy Markdown
Member

Note: Before submitting this pull request, please review our contributing guidelines.

Description

Fixes #557.

Web.json() serializes with faust.utils.json.dumps, which returns str with stdlib json but bytes with orjson. The two paths produced different content-types:

  • str pathResponse(text=...) → aiohttp appends ; charset=utf-8.
  • bytes path (orjson)Response(body=...) → aiohttp does not add a charset.

So with orjson installed, every JSON response was served as application/json (no charset), while the same app on stdlib json served application/json; charset=utf-8. This explains the reporter-vs-maintainer "can't reproduce" — it depends on whether orjson is installed.

Fix

Build the bytes-path Response directly with an explicit charset="utf-8" (verified: Response(body=..., content_type="application/json", charset="utf-8")application/json; charset=utf-8). The generic bytes() helper is left unchanged — binary responses shouldn't be forced to utf-8.

Tests

  • Updated test_json__from_bytes for the new implementation (builds Response(..., charset="utf-8")).
  • Added test_json_content_type_has_charset_for_both_backends: asserts the real Content-Type header is application/json; charset=utf-8 whether dumps returns bytes (orjson) or str (stdlib).
  • Verified it catches the bug: reverting the fix makes the bytes case assert application/json (no charset).
  • flake8 / black --check / isort --check-only clean.

🤖 Generated with Claude Code


Generated by Claude Code

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

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.15%. Comparing base (3073eb9) to head (26d34f0).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wbarnha
wbarnha added this pull request to the merge queue Jul 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

append Content-Type: application/json; charset=utf-8

1 participant