Fixed bug that the type of rpc result does not support types which allows null. (#3434)

* Fixed bug that the type of rpc result does not support types which allows null.

* Added test cases
This commit is contained in:
李铭昕 2021-03-31 17:52:08 +08:00 committed by GitHub
parent 73167cd0d9
commit f84e87b474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# v2.1.12 - TBD
## Fixed
- [3434](https://github.com/hyperf/hyperf/pull/3434) Fixed bug that the type of rpc result does not support types which allows null.
# v2.1.12 - 2021-03-29
## Fixed

View File

@ -162,6 +162,29 @@ class RpcServiceClientTest extends TestCase
$this->assertEquals($uniqid, $ret);
}
public function testProxyReturnNullableTypeWithNull()
{
$container = $this->createContainer();
/** @var MockInterface $transporter */
$transporter = $container->get(JsonRpcTransporter::class);
$transporter->shouldReceive('setLoadBalancer')
->andReturnSelf();
$transporter->shouldReceive('send')
->andReturnUsing(function ($data) {
$id = json_decode($data, true)['id'];
return json_encode([
'id' => $id,
'result' => null,
]);
});
$factory = new ProxyFactory();
$proxyClass = $factory->createProxy(CalculatorServiceInterface::class);
/** @var CalculatorServiceInterface $service */
$service = new $proxyClass($container, CalculatorServiceInterface::class, 'jsonrpc');
$ret = $service->getString();
$this->assertNull($ret);
}
public function testProxyCallableParameterAndReturnArray()
{
$container = $this->createContainer();

View File

@ -58,6 +58,10 @@ class ServiceClient extends AbstractServiceClient
$response = $this->checkRequestIdAndTryAgain($response, $id);
if (array_key_exists('result', $response)) {
$type = $this->methodDefinitionCollector->getReturnType($this->serviceInterface, $method);
if ($type->allowsNull() && $response['result'] === null) {
return null;
}
return $this->normalizer->denormalize($response['result'], $type->getName());
}