Crash on jsonrpc/methods route

Created on 11 September 2024, 4 months ago
Updated 12 September 2024, 4 months ago

Problem/Motivation

Since version 2.1.0 I get a crash on the route jsonrpc/methods:

The website encountered an unexpected error. Try again later.<br><br><em
        class="placeholder">Error</em>: Call to a member function normalize() on null in <em class="placeholder">Drupal\jsonrpc_discovery\Normalizer\AnnotationNormalizer-&gt;normalize()</em> (line
<em class="placeholder">63</em> of <em class="placeholder">modules/contrib/jsonrpc/modules/jsonrpc_discovery/src/Normalizer/AnnotationNormalizer.php</em>).
<pre class="backtrace">Symfony\Component\Serializer\Serializer-&gt;normalize(Object, &#039;json&#039;, Array) (Line: 177)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 177)
Symfony\Component\Serializer\Serializer-&gt;normalize(Array, &#039;json&#039;, Array) (Line: 138)
Symfony\Component\Serializer\Serializer-&gt;serialize(Array, &#039;json&#039;, Array) (Line: 67)
Drupal\jsonrpc_discovery\Controller\DiscoveryController-&gt;methods()
call_user_func_array(Array, Array) (Line: 123)

The code:

 public function normalize($object, $format = NULL, array $context = []): array|\ArrayObject|bool|float|int|string|null {
    $attributes = [];
    foreach ($object as $key => $value) {
      switch ($key) {
        case 'id':
        case 'call':
        case 'access':
          break;

        default:
          $child = $value instanceof AnnotationInterface ? $value->get() : $value;
          if (isset($context[static::DEPTH_KEY]) && $child instanceof AnnotationInterface || (is_array($child)) && Inspector::assertAllObjects($child, AnnotationInterface::class)) {
            if ($context[static::DEPTH_KEY] === 0) {
              break;
            }
            $context[static::DEPTH_KEY] -= 1;
          }
--->    $attributes[$key] = $this->normalizer->normalize($child, $format, $context);
      }
    }
 

In the debugger I could see that the key was 'output' and that $this->normalizer was indeed NULL.

The method annotation that was being processed at the time does not mention 'output':

 * @JsonRpcMethod(
 *     id="my_module.checkExpoTokenApi",
 *     usage=@Translation("Check expo token exists"),
 *     params={
 *         "expo_token": @JsonRpcParameterDefinition(
 *             schema={"type": "string"},
 *             required=true,
 *             description=@Translation("The expo device token.")
 *         ),
 *     }
 * ),

But I remember that the result of the outputSchema() method is set dynamically at some point so the structure of the output appears in the method's self-documentation.

In \Drupal\jsonrpc\Plugin\JsonRpcMethodManager::alterDefinitions, there's

      $class = $method->getClass();
      $output_schema = $class::outputSchema();
      $method->output = $output_schema;

Steps to reproduce

Request jsonrpc/methods

🐛 Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

🇩🇪Germany cspitzlay 🇩🇪🇪🇺

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