mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
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:
parent
73167cd0d9
commit
f84e87b474
@ -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
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user