Skip to content

Commit

Permalink
Merge pull request #715 from dunglas/negotiation_compat
Browse files Browse the repository at this point in the history
Compatbility with Negotiation 2.0.3
  • Loading branch information
dunglas committed Aug 30, 2016
2 parents 90f8688 + 64b5a89 commit b09c9ff
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"symfony/http-kernel": "^2.7 || ^3.0",
"symfony/property-info": "^3.1",
"symfony/serializer": "^3.1",
"willdurand/negotiation": "^2.0"
"willdurand/negotiation": "^2.0.3"
},
"require-dev": {
"behat/behat": "^3.1",
Expand Down
2 changes: 1 addition & 1 deletion features/security/validate_response_types.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Feature: Validate response types
And I send a "GET" request to "/dummies/1"
Then the response status code should be 406
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
And the JSON node "detail" should be equal to 'The "invalid" MIME type is invalid.'
And the JSON node "detail" should be equal to 'Requested format "invalid" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json".'
9 changes: 2 additions & 7 deletions src/EventListener/AddFormatListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace ApiPlatform\Core\EventListener;

use Negotiation\Exception\InvalidMediaType;
use Negotiation\Negotiator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand Down Expand Up @@ -56,12 +55,8 @@ public function onKernelRequest(GetResponseEvent $event)
// First, try to guess the format from the Accept header
$accept = $request->headers->get('Accept');
if (null !== $accept) {
try {
if (null === $acceptHeader = $this->negotiator->getBest($accept, $mimeTypes)) {
throw $this->getNotAcceptableHttpException($accept, $mimeTypes);
}
} catch (InvalidMediaType $e) {
throw new NotAcceptableHttpException(sprintf('The "%s" MIME type is invalid.', $accept));
if (null === $acceptHeader = $this->negotiator->getBest($accept, $mimeTypes)) {
throw $this->getNotAcceptableHttpException($accept, $mimeTypes);
}

$request->setRequestFormat($request->getFormat($acceptHeader->getType()));
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/AddFormatListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testUnsupportedAcceptHeader()

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
* @expectedExceptionMessage The "invalid" MIME type is invalid.
* @expectedExceptionMessage Requested format "invalid" is not supported. Supported MIME types are "application/json".
*/
public function testInvalidAcceptHeader()
{
Expand Down

0 comments on commit b09c9ff

Please sign in to comment.