Problem/Motivation

I am getting the following exception that results in 500 responses on the API rtesponses.
As this happens in production, it is not easy to debug at the moment...

Error: Call to a member function getTypeName() on null in Drupal\jsonapi\JsonApiResource\ResourceIdentifier->__construct() (line 83 of /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/JsonApiResource/ResourceIdentifier.php)
 #0 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/JsonApiResource/ResourceIdentifier.php(299): Drupal\jsonapi\JsonApiResource\ResourceIdentifier->__construct(NULL, '16200c62-496d-4...', Array)
 #1 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/JsonApiResource/ResourceIdentifier.php(319): Drupal\jsonapi\JsonApiResource\ResourceIdentifier::toResourceIdentifier(Object(Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem))
 #2 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/EntityReferenceFieldNormalizer.php(42): Drupal\jsonapi\JsonApiResource\ResourceIdentifier::toResourceIdentifiers(Object(Drupal\Core\Field\EntityReferenceFieldItemList))
 #3 /var/www/FOLDER/vendor/symfony/serializer/Serializer.php(143): Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer->normalize(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 'api_json', Array)
 #4 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 'api_json', Array)
 #5 [internal function]: Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 'api_json', Array)
 #6 /var/www/FOLDER/docroot/modules/contrib/jsonapi_extras/src/SerializerDecorator.php(67): call_user_func_array(Array, Array)
 #7 /var/www/FOLDER/docroot/modules/contrib/jsonapi_extras/src/SerializerDecorator.php(102): Drupal\jsonapi_extras\SerializerDecorator->relay('normalize', Array)
 #8 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/ResourceObjectNormalizer.php(93): Drupal\jsonapi_extras\SerializerDecorator->normalize(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 'api_json', Array)
 #9 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/ResourceObjectNormalizer.php(59): Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->serializeField(Object(Drupal\Core\Field\EntityReferenceFieldItemList), Array, 'api_json')
 #10 /var/www/FOLDER/docroot/modules/contrib/jsonapi_extras/src/Normalizer/ResourceObjectNormalizer.php(37): Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObject), 'api_json', Array)
 #11 /var/www/FOLDER/vendor/symfony/serializer/Serializer.php(143): Drupal\jsonapi_extras\Normalizer\ResourceObjectNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObject), 'api_json', Array)
 #12 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Serializer/Serializer.php(65): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObject), 'api_json', Array)
 #13 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/DataNormalizer.php(26): Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObject), 'api_json', Array)
 #14 [internal function]: Drupal\jsonapi\Normalizer\DataNormalizer->Drupal\jsonapi\Normalizer\{closure}(Object(Drupal\jsonapi\JsonApiResource\ResourceObject))
 #15 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/DataNormalizer.php(27): array_map(Object(Closure), Array)
 #16 /var/www/FOLDER/vendor/symfony/serializer/Serializer.php(143): Drupal\jsonapi\Normalizer\DataNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\IncludedData), 'api_json', Array)
 #17 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\IncludedData), 'api_json', Array)
 #18 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php(201): Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\IncludedData), 'api_json', Array)
 #19 /var/www/FOLDER/vendor/symfony/serializer/Serializer.php(143): Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
 #20 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
 #21 [internal function]: Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
 #22 /var/www/FOLDER/docroot/modules/contrib/jsonapi_extras/src/SerializerDecorator.php(67): call_user_func_array(Array, Array)
 #23 /var/www/FOLDER/docroot/modules/contrib/jsonapi_extras/src/SerializerDecorator.php(102): Drupal\jsonapi_extras\SerializerDecorator->relay('normalize', Array)
 #24 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php(120): Drupal\jsonapi_extras\SerializerDecorator->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
 #25 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php(85): Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\jsonapi\ResourceResponse), Object(Drupal\jsonapi_extras\SerializerDecorator), 'api_json')
 #26 [internal function]: Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->onResponse(Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
 #27 /var/www/FOLDER/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
 #28 /var/www/FOLDER/vendor/symfony/http-kernel/HttpKernel.php(191): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object(Symfony\Component\HttpKernel\Event\FilterResponseEvent))
 #29 /var/www/FOLDER/vendor/symfony/http-kernel/HttpKernel.php(173): Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object(Drupal\jsonapi\ResourceResponse), Object(Symfony\Component\HttpFoundation\Request), 1)
 #30 /var/www/FOLDER/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
 #31 /var/www/FOLDER/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #32 /var/www/FOLDER/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #33 /var/www/FOLDER/docroot/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php(45): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #34 /var/www/FOLDER/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #35 /var/www/FOLDER/docroot/core/modules/ban/src/BanMiddleware.php(50): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #36 /var/www/FOLDER/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #37 /var/www/FOLDER/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #38 /var/www/FOLDER/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
 #39 /var/www/FOLDER/docroot/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true

Proposed resolution

The easiest hot-fix is to check that the resource type is present (not null) in ResourceIdentifier::toResourceIdentifier() before continuing along.

Will provide a PoC patch shortly.

Remaining tasks

TBD.

User interface changes

TBD.

API changes

TBD.

Data model changes

TBD.

Release notes snippet

TBD.

Comments

ndobromirov created an issue. See original summary.

ndobromirov’s picture

This is still running on the contrib version of the module, bit the same missing IF can be placed in core as well.

ndobromirov’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new1006 bytes

Here is a PoC patch that will resolve my issue.
I will post a new one for the contributed module shortly.

ndobromirov’s picture

StatusFileSize
new902 bytes

This is the patch for the contrib module that I am still using...

ndobromirov’s picture

Added the most common issue that will resolve the problem as a top related link:
#2996114: Argument 2 passed to Drupal\jsonapi\Routing\Routes::Drupal\jsonapi\Routing\{closure}() must be an instance of Drupal\jsonapi\ResourceType\ResourceType, NULL given.

Note that this issue is not fixing / preventing it, but just explicitly throwing an exception in case it happens.
Will decide what to do, when I have a locally reproducible scenario for the issue.