function EntityResource::deserialize

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::deserialize()
  2. 10 core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::deserialize()
  3. 11.x core/modules/jsonapi/src/Controller/EntityResource.php \Drupal\jsonapi\Controller\EntityResource::deserialize()

Deserializes a request body, if any.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type for the current request.

\Symfony\Component\HttpFoundation\Request $request: The request object.

string $class: The class into which the request data needs to be deserialized.

string $relationship_field_name: The public relationship field name of the data to be deserialized if the incoming request is for a relationship update. Not required for non- relationship requests.

Return value

array An object normalization.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown if the request body cannot be decoded, or when no request body was provided with a POST or PATCH request.

\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException Thrown if the request body cannot be denormalized.

5 calls to EntityResource::deserialize()
EntityResource::addToRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Adds a relationship to a to-many relationship.
EntityResource::createIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Creates an individual entity.
EntityResource::patchIndividual in core/modules/jsonapi/src/Controller/EntityResource.php
Patches an individual entity.
EntityResource::removeFromRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Deletes the relationship of an entity.
EntityResource::replaceRelationshipData in core/modules/jsonapi/src/Controller/EntityResource.php
Updates the relationship of an entity.

File

core/modules/jsonapi/src/Controller/EntityResource.php, line 830

Class

EntityResource
Process all entity requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function deserialize(ResourceType $resource_type, Request $request, $class, $relationship_field_name = NULL) {
    assert($class === JsonApiDocumentTopLevel::class || $class === ResourceIdentifier::class && !empty($relationship_field_name) && is_string($relationship_field_name));
    $received = (string) $request->getContent();
    if (!$received) {
        assert($request->isMethod('POST') || $request->isMethod('PATCH') || $request->isMethod('DELETE'));
        if ($request->isMethod('DELETE') && $relationship_field_name) {
            throw new BadRequestHttpException(sprintf('You need to provide a body for DELETE operations on a relationship (%s).', $relationship_field_name));
        }
        else {
            throw new BadRequestHttpException('Empty request body.');
        }
    }
    // First decode the request data. We can then determine if the serialized
    // data was malformed.
    try {
        $decoded = $this->serializer
            ->decode($received, 'api_json');
    } catch (UnexpectedValueException $e) {
        // If an exception was thrown at this stage, there was a problem decoding
        // the data. Throw a 400 HTTP exception.
        throw new BadRequestHttpException($e->getMessage());
    }
    try {
        $context = [
            'resource_type' => $resource_type,
        ];
        if ($relationship_field_name) {
            $context['related'] = $resource_type->getInternalName($relationship_field_name);
        }
        return $this->serializer
            ->denormalize($decoded, $class, 'api_json', $context);
    } catch (UnexpectedValueException $e) {
        throw new UnprocessableEntityHttpException($e->getMessage());
    } catch (InvalidArgumentException $e) {
        throw new UnprocessableEntityHttpException($e->getMessage());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.