Return Plain Strings from Formatter

Created on 28 March 2023, over 1 year ago

Problem/Motivation

The name formatter is not working with core non-UI parts of Drupal. These would include the JSONAPI module or REST module.

Steps to reproduce

Install PHP 8.1 (oddly works with PHP 7.4) and Drupal 9.5.x. Enable the JSONAPI module and add a name field to the admin user. Try accessing the JSONAPI url for getting the user `curl http://localhost/jsonapi/user/user/UUID` will return a 500 internal server error with the following in the reporting log.

AssertionError: assert(is_array($normalization) && static::hasNoNestedInstances($normalization) || is_string($normalization) || is_int($normalization) || is_float($normalization) || is_bool($normalization) || is_null($normalization)) in assert() (line 40 of /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php)
#0 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php(40): assert(false, 'assert(is_array...')
#1 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/Value/CacheableNormalization.php(56): Drupal\jsonapi\Normalizer\Value\CacheableNormalization->__construct(Object(Drupal\Core\Cache\CacheableMetadata), Object(Drupal\Component\Render\HtmlEscapedText))
#2 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php(201): Drupal\jsonapi\Normalizer\Value\CacheableNormalization::permanent(Object(Drupal\Component\Render\HtmlEscapedText))
#3 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php(121): Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->serializeField(Object(Drupal\Component\Render\HtmlEscapedText), Array, 'api_json')
#4 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php(73): Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->getNormalization(Array, Object(Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject), 'api_json', Array)
#5 /var/www/drupalvm/blue/vendor/symfony/serializer/Serializer.php(153): Drupal\jsonapi\Normalizer\ResourceObjectNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject), 'api_json', Array)
#6 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject), 'api_json', Array)
#7 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/DataNormalizer.php(26): Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject), 'api_json', Array)
#8 [internal function]: Drupal\jsonapi\Normalizer\DataNormalizer->Drupal\jsonapi\Normalizer\{closure}(Object(Drupal\jsonapi\JsonApiResource\LabelOnlyResourceObject))
#9 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/DataNormalizer.php(27): array_map(Object(Closure), Array)
#10 /var/www/drupalvm/blue/vendor/symfony/serializer/Serializer.php(153): Drupal\jsonapi\Normalizer\DataNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObjectData), 'api_json', Array)
#11 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObjectData), 'api_json', Array)
#12 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php(193): Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\ResourceObjectData), 'api_json', Array)
#13 /var/www/drupalvm/blue/vendor/symfony/serializer/Serializer.php(153): Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
#14 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/Serializer/Serializer.php(62): Symfony\Component\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
#15 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php(120): Drupal\jsonapi\Serializer\Serializer->normalize(Object(Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel), 'api_json', Array)
#16 /var/www/drupalvm/blue/docroot/core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php(85): Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->renderResponseBody(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\jsonapi\CacheableResourceResponse), Object(Drupal\jsonapi\Serializer\Serializer), 'api_json')
#17 [internal function]: Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber->onResponse(Object(Symfony\Component\HttpKernel\Event\ResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#18 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ResponseEvent), 'kernel.response', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#19 /var/www/drupalvm/blue/vendor/symfony/http-kernel/HttpKernel.php(202): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ResponseEvent), 'kernel.response')
#20 /var/www/drupalvm/blue/vendor/symfony/http-kernel/HttpKernel.php(190): Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object(Drupal\jsonapi\CacheableResourceResponse), Object(Symfony\Component\HttpFoundation\Request), 1)
#21 /var/www/drupalvm/blue/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#22 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/www/drupalvm/blue/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/www/drupalvm/blue/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/www/drupalvm/blue/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /var/www/drupalvm/blue/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /var/www/drupalvm/blue/docroot/core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /var/www/drupalvm/blue/docroot/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#32 {main}

The assertion is complaining because the name field is coming back with an object of type `HtmlEscapedText()` and the assertion is valid in that case...

Proposed resolution

Don't return a render object from format parser so it can be used by JSONAPI.

Remaining tasks

User interface changes

None

API changes

Change NameFormatParser::parse to return a string not a MarkupInterface.

Data model changes

None

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States dmlb2000 Richland Washington

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024