Skip to content

fix(state): only emit Content-Type charset for media types that define it#8226

Open
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/content-type-charset-rfc
Open

fix(state): only emit Content-Type charset for media types that define it#8226
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/content-type-charset-rfc

Conversation

@soyuka
Copy link
Copy Markdown
Member

@soyuka soyuka commented Jun 3, 2026

Summary

Per #8218 review feedback, API Platform was unconditionally appending ; charset=utf-8 to every response Content-Type. JSON-based media types (application/json and the RFC 6839 +json structured syntax suffix family: ld+json, hal+json, vnd.api+json, problem+json, merge-patch+json, vnd.openapi+json…) do not define a charset parameter in their IANA registrations, and including one can be rejected by strict clients.

RFC references

  • RFC 8259 §11 (JSON media type registration): "No charset parameter is defined for this registration."
  • RFC 8259 §8.1: JSON exchanged between systems MUST be encoded in UTF-8 — the parameter is redundant.
  • RFC 6839: +json / +yaml structured syntax suffixes inherit the base type's parameter rules; neither defines charset.
  • RFC 2046 §4.1: text/* types define charset.
  • RFC 7303: application/xml and application/xml-external-parsed-entity define charset.

Change

New formatContentType() helper in HttpResponseHeadersTrait that appends ; charset=utf-8 only for text/*, application/xml, and application/xml-external-parsed-entity. Everything else (JSON family, YAML, binary, etc.) returns the bare media type.

Functional tests asserting on Content-Type for JSON / YAML responses were updated accordingly.

Test plan

  • tests/Functional/ full suite green (1562 tests, 16351 assertions)
  • tests/Symfony/Bundle/Test/ClientTest.php green
  • Sample Laravel tests updated
  • CI green on PR

Refs #8218

…e it

JSON-based media types (RFC 8259, RFC 6839 `+json` suffix) do not define
a `charset` parameter, and including one can break strict clients. Restrict
`; charset=utf-8` to `text/*`, `application/xml` and
`application/xml-external-parsed-entity`, matching their IANA registrations.

Updates functional tests asserting on Content-Type accordingly.

Refs api-platform#8218
@soyuka soyuka force-pushed the fix/content-type-charset-rfc branch from 44ebbee to 32e2b1c Compare June 3, 2026 06:34
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.

1 participant