Closed (fixed)
Project:
JSON-RPC
Version:
2.1.0
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
11 Sep 2024 at 09:52 UTC
Updated:
26 Sep 2024 at 01:29 UTC
Jump to comment: Most recent
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->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->normalize(Object, 'json', Array) (Line: 177)
Symfony\Component\Serializer\Serializer->normalize(Array, 'json', Array) (Line: 177)
Symfony\Component\Serializer\Serializer->normalize(Array, 'json', Array) (Line: 138)
Symfony\Component\Serializer\Serializer->serialize(Array, 'json', Array) (Line: 67)
Drupal\jsonrpc_discovery\Controller\DiscoveryController->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;
Request jsonrpc/methods
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
Comment #2
cspitzlayComment #3
ptmkenny commentedComment #5
ptmkenny commentedThank 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.
Comment #6
cspitzlayThanks for your work on this.
I can confirm that MR33 fixes the issue for me.
Comment #7
ptmkenny commentedThank 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.
Comment #8
ptmkenny commentedI'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.
Comment #9
ptmkenny commentedComment #13
ptmkenny commentedFollow-up: #3473712: Add a test for jsonrpc_discovery that verifies the methods returned