Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request format being overwritten to application/problem+json #6885

Closed
SherinBloemendaal opened this issue Dec 22, 2024 · 2 comments
Closed

Comments

@SherinBloemendaal
Copy link

SherinBloemendaal commented Dec 22, 2024

API Platform version(s) affected: 4.0.13
Description
I've come across an issue where the JSON login endpoint and refresh token endpoint stopped working after an error occurred in the API. The error message stated "no username and password" or "no refresh token provided". After digging into the problem, I found the root cause:
When a request is made to an API resource, the ContentNegotiationProvider adds the formats from the config/operation into the Symfony Request object. This works fine for successful responses (2xx), but when an error occurs, the request format keeps its overridden value as application/problem+json. For subsequent requests handled by API-Platform, this is not an issue since the ContentNegotiationProvider is invoked for every request, resetting the formats. However, for any routes outside API-Platform (e.g., refresh token or JWT login endpoints), the ContentNegotiationProvider is not triggered. As a result, the static $formats variable retains the overwritten format application/problem+json, causing application/json to no longer be recognized.

I had this issue before in a PHPUnit WebTestCase and back then managed to get rid of it, but since moving to FrankenPHP worker mode, the problem has returned. It seems this problem only occurs when running in worker mode, which makes sense because the static $formats inside the Symfony Request object is kept in memory.

I've created a reproducer that represents the issue in combination with FrankenPHP in worker mode:
What i've noticed:
Scenario 1: When 406 Not Acceptable is thrown, the json format is being overwritten to:

"json" => [ "application/problem+json" ]

Scenario 2: 500 Internal Server Error is thrown, the jsonld format is being overwritten to:

"jsonld" => [ "application/problem+json" ]

How to reproduce
https://github.com/SherinBloemendaal/api-platform-6885

  1. git clone https://github.com/SherinBloemendaal/api-platform-6885
  2. docker compose build
  3. docker compose exec php composer install
  4. docker compose up -d
  5. Navigate to http://localhost/ to see the scenario's, click the buttons in order to see.

Possible Solution
I think the solution should be created inside Symfony itself, because the kernel should reset the static::$formats in my opinion for subsequent requests. But I am not sure of it.

@norkunas
Copy link
Contributor

This is already being fixed in symfony/symfony#59053

@dunglas
Copy link
Member

dunglas commented Dec 24, 2024

Closing in favor of the upstream issue.

@dunglas dunglas closed this as completed Dec 24, 2024
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

No branches or pull requests

3 participants