Problem/Motivation
The constructor of the class \Drupal\jsonrpc\JsonRpcObject\Error has a parameter that is documented as optional:
* @param mixed $data
* (optional) A primitive or structured value that contains additional
* information about the error. This may be omitted.
But if it is actually omitted then the data property of the error object won't be initialized at all,
which leads to a crash when the data is accessed, as is done by the getData() method:
Error: Typed property Drupal\jsonrpc\JsonRpcObject\Error::$data must not be accessed before initialization in
Drupal\jsonrpc\JsonRpcObject\Error->getData() (line 126 of modules/contrib/jsonrpc/src/JsonRpcObject/Error.php).
Drupal\jsonrpc\Shaper\RpcResponseFactory->doNormalize() (Line: 79)
Drupal\jsonrpc\Shaper\RpcResponseFactory->closure:Drupal\jsonrpc\Shaper\RpcResponseFactory::doTransform():77}()
I'm using PHP 8.4.
Steps to reproduce
Throw a JsonRpcException with an error object that leaves out the optional parameter:
$id = 'whatever';
$jsonrpc_code = -10;
$message = 'hello world';
$error = new Error($jsonrpc_code, $message));
throw JsonRpcException::fromError($error, $id)
Proposed resolution
Remove the "if" in the constructor. Then the property will be set to NULL if left out.
This would currently lead to a NULL value for "data" in the error response, though.
Issue fork jsonrpc-3567234
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
Comment #4
ptmkenny commentedThank you for the detailed report. Let's try your suggestion against the tests.
Comment #5
ptmkenny commentedComment #6
cspitzlayThe tests are green.
Comment #7
ptmkenny commentedYep, I will go ahead and commit this! Thanks for the report and the fix!
Comment #10
cspitzlayThanks.