Skip to content

fix(symfony): retain existing associations of formats to MIME types when adding new MIME types #6833

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

Merged
merged 2 commits into from
Nov 29, 2024

Conversation

dwgebler
Copy link
Contributor

Q A
Branch? 3.4
Tickets Closes #6831
License MIT
Doc PR n/a

Proposed fix for bug where in long-running worker processes e.g. FrankenPHP, the Request formats are overwritten and can lose their default associations such as json --> application/json.

$request->setFormat($format, (array) $mimeTypes);
$existingMimeTypes = $request->getMimeTypes($format);
$newMimeTypes = array_unique(array_merge((array) $mimeTypes, $existingMimeTypes));
$request->setFormat($format, $newMimeTypes);
Copy link
Member

@soyuka soyuka Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we think it'd be best to restore Symfony formats to the value it was initially when the request is done (for example in the ResponseProcessor ?). Doing this may allow some unwanted formats in the next request. ping @dunglas

Copy link
Contributor Author

@dwgebler dwgebler Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we think it'd be best to restore Symfony formats to the value it was initially when the request is done (for example in the ResponseProcessor ?). Doing this may allow some unwanted formats in the next request. ping @dunglas

Yeah I don't know the internals of API Platform well enough to say this fix won't have side-effects on how the system reads MIME types elsewhere, so I can understand the concern. To fix the best way possible I think will unfortunately be a fair bit more effort, because you'll need to read the MIME types for every supported format at the beginning of a request, store that state somewhere, then restore it at the end of every master request. Symfony doesn't provide an interface to iterate or read its internal formats mapping, you can only request it either per-format or per-mimetype on Request's public API.

Edit: having gone through a fair portion of the code today and the test suite, I don't think this change introduces any new problems. API Platform is explicit about injecting the preferred format on every request, so the only issue I can identify is the one this fixes, where request formats are being overwritten in long-running servers rather than expanded. I don't think you will see any benefit to any process of resetting the static Request formats on every request.

This is only a mapping of formats to supported MIME types, and the one(s) you're putting in here will always take precedence over the existing formats, which seems like it should be the behaviour you want. Consider:

array_unique(array_merge(['application/problem+json'],['application/json']));

array(2) {
  [0]=>
  string(24) "application/problem+json"
  [1]=>
  string(16) "application/json"
}

array_unique(array_merge(['application/json'],['application/problem+json', 'application/json']));

array(2) {
  [0]=>
  string(16) "application/json"
  [1]=>
  string(24) "application/problem+json"
}

So you should always be getting the right ordering for the format. You can see this PR hasn't broken your test suite, which is a good sign.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely a good sign as we have some weird tests in there haha

@dwgebler dwgebler force-pushed the fix/6831-3.4 branch 2 times, most recently from 4ae0aa9 to 0665db6 Compare November 28, 2024 00:16
@soyuka soyuka changed the title ContentNegotiationProvider: retain existing associations of formats to MIME types when adding new MIME types fix(symfony): retain existing associations of formats to MIME types when adding new MIME types Nov 29, 2024
@soyuka soyuka merged commit 7c9cca6 into api-platform:3.4 Nov 29, 2024
78 checks passed
@soyuka
Copy link
Member

soyuka commented Nov 29, 2024

Thanks for the patch @dwgebler

soyuka added a commit to soyuka/core that referenced this pull request Dec 1, 2024
soyuka added a commit to soyuka/core that referenced this pull request Dec 1, 2024
soyuka added a commit to soyuka/core that referenced this pull request Dec 1, 2024
soyuka added a commit to soyuka/core that referenced this pull request Dec 1, 2024
soyuka added a commit that referenced this pull request Dec 6, 2024
… types when adding new MIME types (#6833)"

This reverts commit 7c9cca6.
@papsko
Copy link

papsko commented Jul 18, 2025

Is there any reason why this fix isn't mentioned in changelog?
https://api-platform.com/docs/changelog/

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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy