src/EventSubscriber/DefaultExceptionSubscriber.php | 2 +- src/EventSubscriber/ResourceResponseSubscriber.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/EventSubscriber/DefaultExceptionSubscriber.php b/src/EventSubscriber/DefaultExceptionSubscriber.php index 4419b7e..fa9821c 100644 --- a/src/EventSubscriber/DefaultExceptionSubscriber.php +++ b/src/EventSubscriber/DefaultExceptionSubscriber.php @@ -52,7 +52,7 @@ class DefaultExceptionSubscriber extends SerializationDefaultExceptionSubscriber protected function setEventResponse(GetResponseForExceptionEvent $event, $status) { /* @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */ $exception = $event->getException(); - $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ErrorCollection([$exception])), $exception->getStatusCode(), $exception->getHeaders()); + $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ErrorCollection([$exception]), []), $exception->getStatusCode(), $exception->getHeaders()); $response->addCacheableDependency($exception); $event->setResponse($response); } diff --git a/src/EventSubscriber/ResourceResponseSubscriber.php b/src/EventSubscriber/ResourceResponseSubscriber.php index 2461f07..a1634ab 100644 --- a/src/EventSubscriber/ResourceResponseSubscriber.php +++ b/src/EventSubscriber/ResourceResponseSubscriber.php @@ -111,8 +111,11 @@ class ResourceResponseSubscriber implements EventSubscriberInterface { // If there is data to send, serialize and set it as the response body. if ($data !== NULL) { - // First normalize the data. - $jsonapi_doc_object = $serializer->normalize($data, $format, static::generateContext($request)); + // First normalize the data. Note that error responses do not need a + // normalization context, since there are no entities to normalize. + // @see \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::isJsonApiExceptionEvent() + $context = !$response->isSuccessful() ? [] : static::generateContext($request); + $jsonapi_doc_object = $serializer->normalize($data, $format, $context); // Having just normalized the data, we can associate its cacheability with // the response object. assert($jsonapi_doc_object instanceof JsonApiDocumentTopLevelNormalizerValue);