Skip to content

fix(test): default content-type in ApiTestCase matches configured formats#8227

Merged
soyuka merged 2 commits into
api-platform:4.3from
soyuka:fix/apitestcase-default-content-type-7670
Jun 3, 2026
Merged

fix(test): default content-type in ApiTestCase matches configured formats#8227
soyuka merged 2 commits into
api-platform:4.3from
soyuka:fix/apitestcase-default-content-type-7670

Conversation

@soyuka
Copy link
Copy Markdown
Member

@soyuka soyuka commented Jun 3, 2026

Summary

ApiTestCase::createClient() now injects a default Content-Type header derived from the first configured api_platform.formats entry (e.g. application/ld+json) whenever the caller did not provide one. This avoids Symfony's HttpClient defaulting to application/json (added automatically with the json request option) and triggering 415 responses on default API Platform projects where json is not a registered format.

Reproduction

A default API Platform project (only jsonld registered) using self::createClient()->request('POST', '/things', ['json' => [...]]) fails with 415 because the test client lets Symfony stamp Content-Type: application/json.

Test plan

  • Added failing test covering the bug.
  • Test passes after the fix.
  • Related test classes still pass locally.

Fixes #7670

soyuka added 2 commits June 3, 2026 09:32
…mats

Symfony's HttpClient automatically sets "Content-Type: application/json" when the request uses
the "json" option. On a default API Platform project (no explicit formats configured) only
jsonld is registered, so such requests fail with 415 Unsupported Media Type.

ApiTestCase now injects a default Content-Type header derived from the first configured
api_platform.formats entry whenever the caller did not provide one, keeping out-of-the-box
test ergonomics consistent with the configured formats while still letting users override the
header explicitly.

Fixes api-platform#7670
The previous commit injected a default Content-Type from the first configured
api_platform.formats entry on every ApiTestCase request. Because Symfony's
HttpClient merges default headers before the per-request "json" option resolves
its implicit "Content-Type: application/json", that default ended up overriding
application/json for callers that legitimately want it - notably the GraphQL
test trait (POST /graphql with ['json' => $payload]), which then received
"application/ld+json" and was rejected with a 400.

Skip the default whenever "application/json" is among the configured mime types:
in that case Symfony's implicit header already produces a valid request, so
preserving it keeps GraphQL and any "json"-format project working. The original
out-of-the-box case (default API Platform project with only jsonld configured)
is still covered because application/json is not in formats there.
@soyuka soyuka merged commit 71fcb93 into api-platform:4.3 Jun 3, 2026
107 of 108 checks passed
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