Hey there. I'm running Drupal 8.7.9 with jsonapi_extras 8.x-3.12.

When I create a custom endpoint using Drupal Console and call \Drupal::service('jsonapi_extras.entity.to_jsonapi')->normalize($entity); within any of the methods, I get the following error:

Symfony\Component\Serializer\Exception\NotEncodableValueException: Serialization for the format api_json is not supported in Symfony\Component\Serializer\Serializer->serialize() (line 112 of /var/www/html/vendor/symfony/serializer/Serializer.php). Drupal\rest\EventSubscriber\ResourceResponseSubscriber->Drupal\rest\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 177)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object, Object, Object, 'api_json') (Line: 76)
Drupal\rest\EventSubscriber\ResourceResponseSubscriber->onResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 173)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 49)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Here's my test endpoint:

public function get() {
  /** @var \Drupal\user\Entity\User $current_user */
  $current_user = User::load($this->currentUser->id());
  /** @var \Drupal\jsonapi_extras\EntityToJsonApi $entity_to_jsonapi */
  $entity_to_jsonapi = \Drupal::service('jsonapi_extras.entity.to_jsonapi');
  $normalized = $entity_to_jsonapi->normalize($current_user);
  return new ModifiedResourceResponse($normalized, 200);
}

Normalization process works as expected, returning the normalized entity. The error is thrown after my endpoint in vendor/symfony/serializer/Serializer.php:112.

It seems that the serializer is trying to serialize the returned json into api_json.
Could it be that the normalize-methods subrequest is somehow colliding with the main-request making it expect the format api_json instead of json?

Adding the query parameter _format=json and the header accept=application/json to the request doesn't seem make a difference.

In production we're running Drupal 8.7.7 and jsonapi_extras 8.x-3.7 without any issue.

Any help would be appreciated.

Comments

HanPa created an issue. See original summary.

gdiep’s picture

I am encountering the same error after updating JSON:API to latest and installing JSON:API Extras 3.12. Downgrading versions gives varying results but none that have lead to anything positive.

wim leers’s picture

Category: Bug report » Support request
Issue tags: +API-First Initiative

You're combining core/modules/jsonapi infrastructure with core/modules/rest infrastructure. That's not supported.

If you truly need to create custom "endpoints" while using JSON:API, use https://www.drupal.org/project/jsonapi_resources.

wim leers’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.