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

Issue fork jsonrpc-3473531

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cspitzlay created an issue. See original summary.

cspitzlay’s picture

Issue summary: View changes

ptmkenny’s picture

Status: Active » Needs review

Thank you for reporting this. It looks like this was a case of bad renaming; MR33 should fix it for 2.x.

I'm going to try to add a test for this because jsonrpc_discovery has no test that validates the basic output. I think there should be a test that enables jsonrpc_discovery and jsonrpc_core together and then ensures that jsonrpc/methods returns the methods added by jsonrpc_core. I think such a test would've caught this before the release.

cspitzlay’s picture

Thanks for your work on this.

I can confirm that MR33 fixes the issue for me.

ptmkenny’s picture

Thank you for confirming it works. I'm going to make the test assert that we actually get a method as a response before committing this so that hopefully any obvious breakage of the endpoint will be caught immediately in the future.

ptmkenny’s picture

I'm having trouble getting the test to work (the admin user is not seeing the methods added by jsonrpc_core for some reason), so I will create a follow-up issue to extend the test. In the meantime, I am going to prioritize fixing this by making a new release.

ptmkenny’s picture

  • ptmkenny committed f99e14d5 on 2.x
    Issue #3473531 by ptmkenny, cspitzlay: Crash on jsonrpc/methods route
    

  • ptmkenny committed e3f58d64 on 3.x
    Issue #3473531 by ptmkenny, cspitzlay: Crash on jsonrpc/methods route
    
ptmkenny’s picture

Status: Fixed » Closed (fixed)

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