Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09cc0b2

Browse files
committed
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 | 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
2 parents 37dd041 + f7ad285 commit 09cc0b2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,9 @@ public function getMimeType($format)
12381238
*/
12391239
public function getFormat($mimeType)
12401240
{
1241+
$canonicalMimeType = null;
12411242
if (false !== $pos = strpos($mimeType, ';')) {
1242-
$mimeType = substr($mimeType, 0, $pos);
1243+
$canonicalMimeType = substr($mimeType, 0, $pos);
12431244
}
12441245

12451246
if (null === static::$formats) {
@@ -1250,6 +1251,9 @@ public function getFormat($mimeType)
12501251
if (in_array($mimeType, (array) $mimeTypes)) {
12511252
return $format;
12521253
}
1254+
if (null !== $canonicalMimeType && in_array($canonicalMimeType, (array) $mimeTypes)) {
1255+
return $format;
1256+
}
12531257
}
12541258
}
12551259

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ public function testGetMimeTypeFromFormat($format, $mimeTypes)
325325
}
326326
}
327327

328+
public function testGetFormatWithCustomMimeType()
329+
{
330+
$request = new Request();
331+
$request->setFormat('custom', 'application/vnd.foo.api;myversion=2.3');
332+
$this->assertEquals('custom', $request->getFormat('application/vnd.foo.api;myversion=2.3'));
333+
}
334+
328335
public function getFormatToMimeTypeMapProvider()
329336
{
330337
return array(

0 commit comments

Comments
 (0)