diff --git a/core/modules/rest/src/Annotation/RestResource.php b/core/modules/rest/src/Annotation/RestResource.php index 82269dbf567..0af11a84a9b 100644 --- a/core/modules/rest/src/Annotation/RestResource.php +++ b/core/modules/rest/src/Annotation/RestResource.php @@ -38,4 +38,11 @@ class RestResource extends Plugin { */ public $label; + /** + * The serialization class to deserialize serialized data into. + * + * @var string (optional) + */ + public $serialization_class; + } diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 2c3310a80c0..b7cb630852a 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -82,9 +82,15 @@ class RequestHandler implements ContainerAwareInterface, ContainerInjectionInter $request_method = $request->getMethod(); if (in_array($format, $resource_config->getFormats($request_method))) { $definition = $resource->getPluginDefinition(); - $class = $definition['serialization_class']; try { - $unserialized = $serializer->deserialize($received, $class, $format, array('request_method' => $method)); + if (!empty($definition['serialization_class'])) { + $unserialized = $serializer->deserialize($received, $definition['serialization_class'], $format, array('request_method' => $method)); + } + // If the plugin does not specify a serialization class just decode + // the received data. + else { + $unserialized = $serializer->decode($received, $format, array('request_method' => $method)); + } } catch (UnexpectedValueException $e) { $error['error'] = $e->getMessage(); diff --git a/core/modules/rest/src/Tests/ResourceTest.php b/core/modules/rest/src/Tests/ResourceTest.php index e4d6c0026d4..72bed0f4ee7 100644 --- a/core/modules/rest/src/Tests/ResourceTest.php +++ b/core/modules/rest/src/Tests/ResourceTest.php @@ -5,6 +5,7 @@ namespace Drupal\rest\Tests; use Drupal\Core\Session\AccountInterface; use Drupal\rest\RestResourceConfigInterface; use Drupal\user\Entity\Role; +use Drupal\user\RoleInterface; /** * Tests the structure of a REST resource. @@ -18,7 +19,7 @@ class ResourceTest extends RESTTestBase { * * @var array */ - public static $modules = array('hal', 'rest', 'entity_test'); + public static $modules = array('hal', 'rest', 'entity_test', 'rest_test'); /** * The entity. @@ -95,6 +96,22 @@ class ResourceTest extends RESTTestBase { $this->curlClose(); } + /** + * Tests that serialization_class is optional. + */ + public function testSerializationClassIsOptional() { + $this->enableService('serialization_test', 'POST', 'json'); + + Role::load(RoleInterface::ANONYMOUS_ID) + ->grantPermission('restful post serialization_test') + ->save(); + + $serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json'); + $this->httpRequest('serialization_test', 'POST', $serialized, 'application/json'); + $this->assertResponse(200); + $this->assertResponseBody('["foo","bar"]'); + } + /** * Tests that resource URI paths are formatted properly. */ diff --git a/core/modules/rest/tests/modules/rest_test/rest_test.info.yml b/core/modules/rest/tests/modules/rest_test/rest_test.info.yml index b5f49669bb9..e10b2266608 100644 --- a/core/modules/rest/tests/modules/rest_test/rest_test.info.yml +++ b/core/modules/rest/tests/modules/rest_test/rest_test.info.yml @@ -1,6 +1,6 @@ name: 'REST test' type: module -description: 'Provides test hooks for REST module.' +description: 'Provides test hooks and resources for REST module.' package: Testing version: VERSION core: 8.x diff --git a/core/modules/rest/tests/modules/rest_test/src/Plugin/rest/resource/NoSerializationClassTestResource.php b/core/modules/rest/tests/modules/rest_test/src/Plugin/rest/resource/NoSerializationClassTestResource.php new file mode 100644 index 00000000000..3e83a4ce74c --- /dev/null +++ b/core/modules/rest/tests/modules/rest_test/src/Plugin/rest/resource/NoSerializationClassTestResource.php @@ -0,0 +1,32 @@ +