bug #18255 [HttpFoundation] Fix support of custom mime types with parameters (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 | https://github.com/FriendsOfSymfony/FOSRestBundle/issues/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
This commit is contained in:
commit
09cc0b20d5
@ -1238,8 +1238,9 @@ class Request
|
||||
*/
|
||||
public function getFormat($mimeType)
|
||||
{
|
||||
$canonicalMimeType = null;
|
||||
if (false !== $pos = strpos($mimeType, ';')) {
|
||||
$mimeType = substr($mimeType, 0, $pos);
|
||||
$canonicalMimeType = substr($mimeType, 0, $pos);
|
||||
}
|
||||
|
||||
if (null === static::$formats) {
|
||||
@ -1250,6 +1251,9 @@ class Request
|
||||
if (in_array($mimeType, (array) $mimeTypes)) {
|
||||
return $format;
|
||||
}
|
||||
if (null !== $canonicalMimeType && in_array($canonicalMimeType, (array) $mimeTypes)) {
|
||||
return $format;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,6 +325,13 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetFormatWithCustomMimeType()
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setFormat('custom', 'application/vnd.foo.api;myversion=2.3');
|
||||
$this->assertEquals('custom', $request->getFormat('application/vnd.foo.api;myversion=2.3'));
|
||||
}
|
||||
|
||||
public function getFormatToMimeTypeMapProvider()
|
||||
{
|
||||
return array(
|
||||
|
Reference in New Issue
Block a user