diff -u b/src/Controller/EntityResource.php b/src/Controller/EntityResource.php --- b/src/Controller/EntityResource.php +++ b/src/Controller/EntityResource.php @@ -96,4 +96,13 @@ /** + * Returns The JSON API resource type of this resource. + * + * @return \Drupal\jsonapi\ResourceType\ResourceType + */ + public function getResourceType() { + return $this->resourceType; + } + + /** * Gets the individual entity. * diff -u b/src/Controller/RequestHandler.php b/src/Controller/RequestHandler.php --- b/src/Controller/RequestHandler.php +++ b/src/Controller/RequestHandler.php @@ -5,7 +5,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Render\RenderContext; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\jsonapi\Context\CurrentContext; +use Drupal\jsonapi\ResourceType\ResourceType; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -48,14 +48,12 @@ public function handle(RouteMatchInterface $route_match, Request $request) { $method = strtolower($request->getMethod()); $route = $route_match->getRouteObject(); + $resource = $this->resourceFactory($route); // Deserialize incoming data if available. /* @var \Symfony\Component\Serializer\SerializerInterface $serializer */ $serializer = $this->container->get('serializer'); - /* @var \Drupal\jsonapi\Context\CurrentContext $current_context */ - $current_context = $this->container->get('jsonapi.current_context'); - $current_context->reset(); - $unserialized = $this->deserializeBody($request, $serializer, $route->getOption('serialization_class'), $current_context); + $unserialized = $this->deserializeBody($request, $serializer, $route->getOption('serialization_class'), $resource->getResourceType()); if ($unserialized instanceof Response && !$unserialized->isSuccessful()) { return $unserialized; } @@ -74,7 +72,6 @@ // Invoke the operation on the resource plugin. $action = $this->action($route_match, $method); - $resource = $this->resourceFactory($route); // Only add the unserialized data if there is something there. $extra_parameters = $unserialized ? [$unserialized, $request] : [$request]; @@ -104,13 +101,13 @@ * The serializer for the deserialization of the input data. * @param string $serialization_class * The class the input data needs to deserialize into. - * @param \Drupal\jsonapi\Context\CurrentContext $current_context - * The current context. + * @param \Drupal\jsonapi\ResourceType\ResourceType + * The resource type of the data to deserialize. * * @return mixed * The deserialized data or a Response object in case of error. */ - public function deserializeBody(Request $request, SerializerInterface $serializer, $serialization_class, CurrentContext $current_context) { + public function deserializeBody(Request $request, SerializerInterface $serializer, $serialization_class, ResourceType $resource_type) { $received = $request->getContent(); if (empty($received) || $request->isMethodCacheable()) { return NULL; @@ -119,8 +116,8 @@ try { return $serializer->deserialize($received, $serialization_class, $format, [ 'related' => $request->get('related'), - 'target_entity' => $request->get($current_context->getResourceType()->getEntityTypeId()), - 'resource_type' => $current_context->getResourceType(), + 'target_entity' => $request->get($resource_type->getEntityTypeId()), + 'resource_type' => $resource_type, ]); } catch (UnexpectedValueException $e) { only in patch2: unchanged: --- a/tests/src/Unit/Controller/RequestHandlerTest.php +++ b/tests/src/Unit/Controller/RequestHandlerTest.php @@ -39,15 +39,12 @@ class RequestHandlerTest extends UnitTestCase { ->willThrow(new UnexpectedValueException('Foo')); $serializer->serialize(Argument::any(), Argument::any(), Argument::any()) ->willReturn('{"errors":[{"status":422,"message":"Foo"}]}'); - $current_context = $this->prophesize(CurrentContext::class); - $current_context->getResourceType() - ->willReturn(new ResourceType($this->randomMachineName(), $this->randomMachineName(), NULL)); try { $request_handler->deserializeBody( $request->reveal(), $serializer->reveal(), 'invalid', - $current_context->reveal() + new ResourceType($this->randomMachineName(), $this->randomMachineName(), NULL) ); $this->fail('Expected exception.'); }