From 52a5d827dcfe8807c326807286fa8da1ecbc4d14 Mon Sep 17 00:00:00 2001 From: Ener-Getick Date: Mon, 21 Mar 2016 20:12:06 +0100 Subject: [PATCH] Pass the raw mime type to Request --- DependencyInjection/FOSRestExtension.php | 4 ---- EventListener/FormatListener.php | 2 +- EventListener/MimeTypeListener.php | 4 +++- Tests/Functional/VersionTest.php | 4 ++-- Tests/Functional/app/Version/config.yml | 5 ++++- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/DependencyInjection/FOSRestExtension.php b/DependencyInjection/FOSRestExtension.php index 6f135536e..5a423d7f1 100644 --- a/DependencyInjection/FOSRestExtension.php +++ b/DependencyInjection/FOSRestExtension.php @@ -154,10 +154,6 @@ private function loadFormatListener(array $config, XmlFileLoader $loader, Contai 'fos_rest.format_listener.rules', $config['format_listener']['rules'] ); - - if ($config['view']['mime_types']['enabled'] && !method_exists(Request::class, 'getMimeTypes')) { - $container->getDefinition('fos_rest.format_negotiator')->addArgument($config['view']['mime_types']['formats']); - } } } diff --git a/EventListener/FormatListener.php b/EventListener/FormatListener.php index faba288b2..fed2c6393 100644 --- a/EventListener/FormatListener.php +++ b/EventListener/FormatListener.php @@ -59,7 +59,7 @@ public function onKernelRequest(GetResponseEvent $event) if (null === $format) { $accept = $this->formatNegotiator->getBest(''); if (null !== $accept && 0.0 < $accept->getQuality()) { - $format = $request->getFormat($accept->getType()); + $format = $request->getFormat($accept->getValue()); if (null !== $format) { $request->attributes->set('media_type', $accept->getValue()); } diff --git a/EventListener/MimeTypeListener.php b/EventListener/MimeTypeListener.php index d239ea53b..d31a23e7f 100644 --- a/EventListener/MimeTypeListener.php +++ b/EventListener/MimeTypeListener.php @@ -58,7 +58,9 @@ public function onKernelRequest(GetResponseEvent $event) } elseif (null !== $request->getMimeType($format)) { $class = new \ReflectionClass(Request::class); $properties = $class->getStaticProperties(); - $mimeTypes = array_merge($mimeTypes, $properties['formats'][$format]); + if (isset($properties['formats'][$format])) { + $mimeTypes = array_merge($mimeTypes, $properties['formats'][$format]); + } } $request->setFormat($format, $mimeTypes); diff --git a/Tests/Functional/VersionTest.php b/Tests/Functional/VersionTest.php index 65a48137e..44a931a25 100644 --- a/Tests/Functional/VersionTest.php +++ b/Tests/Functional/VersionTest.php @@ -30,7 +30,7 @@ public function testCustomHeaderVersion() '/version?query_version=3.2', [], [], - ['HTTP_Version-Header' => '2.1', 'HTTP_Accept' => 'application/json;myversion=2.3'] + ['HTTP_Version-Header' => '2.1', 'HTTP_Accept' => 'application/vnd.foo.api+json;myversion=2.3'] ); $this->assertEquals('{"version":"2.1"}', $this->client->getResponse()->getContent()); } @@ -54,7 +54,7 @@ public function testAcceptHeaderVersion() '/version?query_version=3.2', [], [], - ['HTTP_Accept' => 'application/json;myversion=2.3'] + ['HTTP_Accept' => 'application/vnd.foo.api+json;myversion=2.3'] ); $this->assertEquals('{"version":"2.3"}', $this->client->getResponse()->getContent()); } diff --git a/Tests/Functional/app/Version/config.yml b/Tests/Functional/app/Version/config.yml index e5d93ab78..914c37840 100644 --- a/Tests/Functional/app/Version/config.yml +++ b/Tests/Functional/app/Version/config.yml @@ -15,7 +15,10 @@ fos_rest: view: view_response_listener: true mime_types: - json: ['application/json', 'application/json;myversion=2.3'] + json: + - application/json + - application/vnd.foo.api+json;myversion=2.3 + - application/vnd.foo.api+json # Fix for https://github.com/FriendsOfSymfony/FOSRestBundle/issues/1399 versioning: enabled: true default_version: 3.4.2