function JsonApiRequestValidator::onResponse

Same name in other branches
  1. 10 core/modules/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php \Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator::onResponse()

Validates JSON:API requests.

Parameters

\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The event to process.

File

core/modules/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php, line 47

Class

JsonApiRequestValidator
Subscriber that validates the query parameter names on a JSON:API request.

Namespace

Drupal\jsonapi\EventSubscriber

Code

public function onResponse(ResponseEvent $event) {
    $request = $event->getRequest();
    if ($request->getRequestFormat() !== 'api_json') {
        return;
    }
    // At this point, we've already run validation on the request by checking
    // the query arguments. This means that if the query arguments change, we
    // may need to run this validation again. Therefore, all responses need to
    // vary by url.query_args.
    $response = $event->getResponse();
    if ($response instanceof CacheableResponseInterface) {
        $response->addCacheableDependency((new CacheableMetadata())->addCacheContexts([
            'url.query_args',
        ]));
    }
}

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