diff --git a/core/modules/rest/lib/Drupal/rest/RequestHandler.php b/core/modules/rest/lib/Drupal/rest/RequestHandler.php index b7affb4..3484417 100644 --- a/core/modules/rest/lib/Drupal/rest/RequestHandler.php +++ b/core/modules/rest/lib/Drupal/rest/RequestHandler.php @@ -42,9 +42,8 @@ public function handle(Request $request, $id = NULL) { if (!empty($received)) { $definition = $resource->getDefinition(); $class = $definition['serialization_class']; - // @todo Replace the format here with something we get from the HTTP - // Content-type header. See http://drupal.org/node/1850704 - $unserialized = $serializer->deserialize($received, $class, 'drupal_jsonld'); + $format = $request->getContentType(); + $unserialized = $serializer->deserialize($received, $class, $format); } // Invoke the operation on the resource plugin. @@ -58,8 +57,11 @@ public function handle(Request $request, $id = NULL) { // Serialize the outgoing data for the response, if available. $data = $response->getResponseData(); if ($data != NULL) { + // All REST routes are restricted to exactly one format, so instead of + // parsing it out of the Accept headers again we can simply retrieve the + // format requirement. $format = $request->attributes->get('_route')->getRequirement('_format'); - // If there is no format associated just pick Drupal JSON. + // If there is no format associated just pick Drupal JSON-LD. if (!$format) { $format = 'drupal_jsonld'; } diff --git a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php index b47f296..d25b776 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php @@ -38,7 +38,7 @@ public function testCreate() { // entity types here as well. $entity_type = 'entity_test'; - $this->enableService('entity:' . $entity_type); + $this->enableService('entity:' . $entity_type, 'POST'); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful post entity:' . $entity_type)); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php b/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php index 66f85c4..cd61bab 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/DeleteTest.php @@ -36,7 +36,7 @@ public function testDelete() { // Define the entity types we want to test. $entity_types = array('entity_test', 'node', 'user'); foreach ($entity_types as $entity_type) { - $this->enableService('entity:' . $entity_type); + $this->enableService('entity:' . $entity_type, 'DELETE'); // Create a user account that has the required permissions to delete // resources via the web API. $account = $this->drupalCreateUser(array('restful delete entity:' . $entity_type)); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index 0b41249..84c3bde 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -163,7 +163,7 @@ protected function entityValues($entity_type) { * @param string $format * (Optional) The serialization format, e.g. jsonld. */ - protected function enableService($resource_type, $method, $format = NULL) { + protected function enableService($resource_type, $method = 'GET', $format = NULL) { // Enable web API for this entity type. $config = config('rest.settings'); $settings = array(); diff --git a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php index 83729aa..669a265 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php @@ -38,7 +38,7 @@ public function testPatchUpdate() { // entity types here as well. $entity_type = 'entity_test'; - $this->enableService('entity:' . $entity_type); + $this->enableService('entity:' . $entity_type, 'PATCH'); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful patch entity:' . $entity_type)); @@ -106,7 +106,7 @@ public function testPutUpdate() { // entity types here as well. $entity_type = 'entity_test'; - $this->enableService('entity:' . $entity_type); + $this->enableService('entity:' . $entity_type, 'PUT'); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful put entity:' . $entity_type));