fix(test): default content-type in ApiTestCase matches configured formats#8227
Merged
soyuka merged 2 commits intoJun 3, 2026
Merged
Conversation
…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.
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.
Summary
ApiTestCase::createClient()now injects a defaultContent-Typeheader derived from the first configuredapi_platform.formatsentry (e.g.application/ld+json) whenever the caller did not provide one. This avoids Symfony's HttpClient defaulting toapplication/json(added automatically with thejsonrequest option) and triggering 415 responses on default API Platform projects wherejsonis not a registered format.Reproduction
A default API Platform project (only
jsonldregistered) usingself::createClient()->request('POST', '/things', ['json' => [...]])fails with 415 because the test client lets Symfony stampContent-Type: application/json.Test plan
Fixes #7670