diff --git a/json_rpc/client.nim b/json_rpc/client.nim index 3df1e10..5b52adc 100644 --- a/json_rpc/client.nim +++ b/json_rpc/client.nim @@ -96,10 +96,11 @@ proc processMessage*(client: RpcClient, line: string): Result[void, string] = requestFut.fail(newException(JsonRpcError, error)) return ok() - if response.result.isNone: + # Up to this point, the result should contains something + if response.result.string.len == 0: return err("missing or invalid response result") - requestFut.complete(response.result.get) + requestFut.complete(response.result) return ok() except CatchableError as exc: diff --git a/json_rpc/private/jrpc_sys.nim b/json_rpc/private/jrpc_sys.nim index c118611..e81621f 100644 --- a/json_rpc/private/jrpc_sys.nim +++ b/json_rpc/private/jrpc_sys.nim @@ -103,7 +103,7 @@ type ResponseRx* = object jsonrpc*: results.Opt[JsonRPC2] id* : results.Opt[RequestId] - result* : results.Opt[JsonString] + result* : JsonString error* : results.Opt[ResponseError] ReBatchKind* = enum diff --git a/tests/test_callsigs.nim b/tests/test_callsigs.nim index 6a6c565..b4e28a8 100644 --- a/tests/test_callsigs.nim +++ b/tests/test_callsigs.nim @@ -18,6 +18,13 @@ template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] type Variant = int | bool | string + RefObject = ref object + name: string + +template derefType(T: type): untyped = + typeof(T()[]) + +derefType(RefObject).useDefaultSerializationIn JrpcConv createRpcSigs(RpcClient, sourceDir & "/private/file_callsigs.nim") @@ -33,6 +40,7 @@ createRpcSigsFromNim(RpcClient): proc get_Name(id: int): string proc getJsonString(name: string): JsonString proc getVariant(id: Variant): bool + proc getRefObject(shouldNull: bool): RefObject proc installHandlers(s: RpcServer) = s.rpc("shh_uninstallFilter") do(id: int) -> bool: @@ -78,6 +86,10 @@ proc installHandlers(s: RpcServer) = return "moo" return "meow" + s.rpc("getRefObject") do(shouldNull: bool) -> Refobject: + if shouldNull: return nil + return RefObject(name: "meow") + suite "test callsigs": var server = newRpcSocketServer(["127.0.0.1:0"]) server.installHandlers() @@ -130,5 +142,13 @@ suite "test callsigs": let res4 = waitFor client.getVariant(33) check res4 == true + test "Handle null return value correctly": + let res = waitFor client.getRefObject(true) + check res.isNil + + let res2 = waitFor client.getRefObject(false) + check res2.isNil.not + check res2.name == "meow" + server.stop() waitFor server.closeWait() diff --git a/tests/test_jrpc_sys.nim b/tests/test_jrpc_sys.nim index 44e76ad..94f1ffa 100644 --- a/tests/test_jrpc_sys.nim +++ b/tests/test_jrpc_sys.nim @@ -174,8 +174,8 @@ suite "jrpc_sys conversion": rx.jsonrpc.isSome rx.id.isSome rx.id.get.num == 777 - rx.result.isSome - rx.result.get == JsonString("true") + rx.result.string.len > 0 + rx.result == JsonString("true") rx.error.isNone test "ResponseTx -> ResponseRx: id(string), err: nodata": @@ -186,7 +186,7 @@ suite "jrpc_sys conversion": rx.jsonrpc.isSome rx.id.isSome rx.id.get.str == "gum" - rx.result.isNone + rx.result.string.len == 0 rx.error.isSome rx.error.get.code == 999 rx.error.get.message == "fatal" @@ -200,7 +200,7 @@ suite "jrpc_sys conversion": rx.jsonrpc.isSome rx.id.isSome rx.id.get.str == "gum" - rx.result.isNone + rx.result.string.len == 0 rx.error.isSome rx.error.get.code == 999 rx.error.get.message == "fatal"