Skip to content

[HttpFoundation] Fix support of custom mime types with parameters #18255

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 1 commit into from
Mar 25, 2016

Conversation

GuilhemN
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets FriendsOfSymfony/FOSRestBundle#1399
License MIT

When using mime types with parameters, getFormat won't return the expected format as illustrated:

$request = new Request();
$request->setFormat('custom', 'app/foo;param=bar');

$request->getFormat('app/foo;param=bar'); 
// will return null as the parameters are removed

So my proposal is to search the format corresponding to a mime type with its raw value or with the its parameters removed.

@GuilhemN GuilhemN changed the title [Request] Fix support of custom mime types with parameters [HttpFoundation] Fix support of custom mime types with parameters Mar 21, 2016
if (in_array($mimeType, (array) $mimeTypes)) {
if (in_array($extendedMimeType, (array) $mimeTypes)) {
return $format;
} elseif (isset($mimeType) && in_array($mimeType, (array) $mimeTypes)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be just if not elseif as you call return in first if.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thank you

*
* @return string|null The format (null if not found)
*/
public function getFormat($mimeType)
public function getFormat($extendedMimeType)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's necessary to change this variable name. $mimeType should be enough. It doesn't matter if the MIME type has parameters or not, both are MIME types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would we call the $mimeType without parameters then?

Copy link
Member

Choose a reason for hiding this comment

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

From the outside of this method, we always pass a MIME type, so I think that the argument should be called $mimeType. Internally you also check the MIME type without arguments, so maybe that could be called $canonicalMimeType ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 changed.

@GuilhemN GuilhemN force-pushed the MIMETYPE branch 2 times, most recently from 70d2130 to e75d7f0 Compare March 23, 2016 17:10
@fabpot
Copy link
Member

fabpot commented Mar 25, 2016

👍

@@ -1250,6 +1250,9 @@ public function getFormat($mimeType)
if (in_array($mimeType, (array) $mimeTypes)) {
return $format;
}
if (isset($canonicalMimeType) && in_array($canonicalMimeType, (array) $mimeTypes)) {
Copy link
Member

Choose a reason for hiding this comment

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

I would probably rather initialise $canonicalMimeType with null and omit the isset() check.

Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

Choose a reason for hiding this comment

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

But this would force to call in_array() which is slower in case there is no cannonical mime-type, the isset() here prevents this useless check (in_array(null, (array) $mimeTypes)).

Instead of isset() when the variable is initialized with null, could be strict check:

if (null !== $canonicalMimeType && in_array($canonicalMimeType, (array) $mimeTypes)) {}

Copy link
Member

Choose a reason for hiding this comment

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

yes, that check should be added then instead of isset()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

@xabbuh
Copy link
Member

xabbuh commented Mar 25, 2016

👍

Status: Reviewed

@fabpot
Copy link
Member

fabpot commented Mar 25, 2016

Thank you @Ener-Getick.

@fabpot fabpot merged commit f7ad285 into symfony:2.3 Mar 25, 2016
fabpot added a commit that referenced this pull request Mar 25, 2016
…ameters (Ener-Getick)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpFoundation] Fix support of custom mime types with parameters

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | FriendsOfSymfony/FOSRestBundle#1399
| License       | MIT

When using mime types with parameters, ``getFormat`` won't return the expected format as illustrated:
```php
$request = new Request();
$request->setFormat('custom', 'app/foo;param=bar');

$request->getFormat('app/foo;param=bar');
// will return null as the parameters are removed
```

So my proposal is to search the format corresponding to a mime type with its raw value or with the its parameters removed.

Commits
-------

f7ad285 [Request] Fix support of custom mime types with parameters
@GuilhemN GuilhemN deleted the MIMETYPE branch March 25, 2016 16:29
@fabpot fabpot mentioned this pull request Apr 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants