The serialization fails if used with Views REST export

Created on 24 May 2023, about 1 year ago
Updated 25 September 2023, 9 months ago

Problem/Motivation

If the $first_row in Xls::extractHeaders is a single string, a PHP error is generated.

The website encountered an unexpected error. Please try again later.
TypeError: array_keys(): Argument #1 ($array) must be of type array, string given in array_keys() (line 313 of modules/contrib/xls_serialization/src/Encoder/Xls.php).

This can happen if the user isn't authenticated to a View REST export that uses this serialization.

See the core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php function on4xx that executes the serialize command

$format = $request->getRequestFormat();
$content = ['message' => $exception->getMessage()];
$encoded_content = $this->serializer->serialize($content, $format);

Steps to reproduce

  1. Create a REST export View with access defined so that you have user that cannot access the REST export.
  2. Configure generally the REST export to work if user is authenticated. Set URL for the REST export.
  3. Now with a user that doesn't have access to that new REST export, try to access it directly with URL of the REST export.
  4. PHP error occurred.

Proposed resolution

Support cases when the content contains error message.

Remaining tasks

Write patch to ensure serialization doesn't fail if user is unauthenticated to use views REST export.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.3

Component

Code

Created by

🇫🇮Finland MikaT

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

Comments & Activities

  • Issue created by @MikaT
  • 🇪🇸Spain ajimenezz

    Hello, is there some solution to this?

    I have the same error here:

    The website encountered an unexpected error. Please try again later.
    TypeError: array_keys(): Argument #1 ($array) must be of type array, string given in array_keys() (line 313 of modules/xls_serialization/src/Encoder/Xls.php).
    array_keys() (Line: 313)
    Drupal\xls_serialization\Encoder\Xls->extractHeaders() (Line: 169)
    Drupal\xls_serialization\Encoder\Xls->setHeaders() (Line: 75)
    Drupal\xls_serialization\Encoder\Xls->encode() (Line: 40)
    Symfony\Component\Serializer\Encoder\ChainEncoder->encode() (Line: 306)
    Symfony\Component\Serializer\Serializer->encode() (Line: 129)
    Symfony\Component\Serializer\Serializer->serialize() (Line: 77)
    Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber->on4xx() (Line: 112)
    Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase->onException()
    call_user_func() (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 219)
    Symfony\Component\HttpKernel\HttpKernel->handleThrowable() (Line: 91)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 57)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 52)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
    Stack\StackedHttpKernel->handle() (Line: 717)
    Drupal\Core\DrupalKernel->handle() (Line: 19)

  • 🇨🇦Canada danrod

    I am having the same issue here, I'll try to submit a patch today.

Production build 0.69.0 2024