Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC server handle null return value correctly #189

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions json_rpc/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion json_rpc/private/jrpc_sys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_callsigs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
8 changes: 4 additions & 4 deletions tests/test_jrpc_sys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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"
Expand All @@ -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"
Expand Down