Issue #2419825 by marthinal, Wim Leers, bigjim, dawehner, klausi: Make serialization_class optional
parent
3993ef3313
commit
b3d0a73198
|
@ -38,4 +38,11 @@ class RestResource extends Plugin {
|
|||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* The serialization class to deserialize serialized data into.
|
||||
*
|
||||
* @var string (optional)
|
||||
*/
|
||||
public $serialization_class;
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\rest_test\Plugin\rest\resource;
|
||||
|
||||
use Drupal\rest\Plugin\ResourceBase;
|
||||
use Drupal\rest\ResourceResponse;
|
||||
|
||||
/**
|
||||
* Class used to test that serialization_class is optional.
|
||||
*
|
||||
* @RestResource(
|
||||
* id = "serialization_test",
|
||||
* label = @Translation("Optional serialization_class"),
|
||||
* serialization_class = "",
|
||||
* uri_paths = {}
|
||||
* )
|
||||
*/
|
||||
class NoSerializationClassTestResource extends ResourceBase {
|
||||
|
||||
/**
|
||||
* Responds to a POST request.
|
||||
*
|
||||
* @param array $data
|
||||
* An array with the payload.
|
||||
*
|
||||
* @return \Drupal\rest\ResourceResponse
|
||||
*/
|
||||
public function post(array $data = []) {
|
||||
return new ResourceResponse($data);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue