Skip to content

Commit 1db978c

Browse files
fjlJasonYuan869
andauthored
rpc: fix unmarshaling of null result in CallContext (#26723)
The change fixes unmarshaling of JSON null results into json.RawMessage. --------- Co-authored-by: Jason Yuan <[email protected]> Co-authored-by: Jason Yuan <[email protected]>
1 parent 7c749c9 commit 1db978c

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

rpc/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,10 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
345345
case len(resp.Result) == 0:
346346
return ErrNoResult
347347
default:
348-
return json.Unmarshal(resp.Result, &result)
348+
if result == nil {
349+
return nil
350+
}
351+
return json.Unmarshal(resp.Result, result)
349352
}
350353
}
351354

rpc/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ func TestClientResponseType(t *testing.T) {
6969
}
7070
}
7171

72+
// This test checks calling a method that returns 'null'.
73+
func TestClientNullResponse(t *testing.T) {
74+
server := newTestServer()
75+
defer server.Stop()
76+
77+
client := DialInProc(server)
78+
defer client.Close()
79+
80+
var result json.RawMessage
81+
if err := client.Call(&result, "test_null"); err != nil {
82+
t.Fatal(err)
83+
}
84+
if result == nil {
85+
t.Fatal("Expected non-nil result")
86+
}
87+
if !reflect.DeepEqual(result, json.RawMessage("null")) {
88+
t.Errorf("Expected null, got %s", result)
89+
}
90+
}
91+
7292
// This test checks that server-returned errors with code and data come out of Client.Call.
7393
func TestClientErrorData(t *testing.T) {
7494
server := newTestServer()

rpc/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestServerRegisterName(t *testing.T) {
4545
t.Fatalf("Expected service calc to be registered")
4646
}
4747

48-
wantCallbacks := 12
48+
wantCallbacks := 13
4949
if len(svc.callbacks) != wantCallbacks {
5050
t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks))
5151
}

rpc/testservice_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func (o *MarshalErrObj) MarshalText() ([]byte, error) {
7878

7979
func (s *testService) NoArgsRets() {}
8080

81+
func (s *testService) Null() any {
82+
return nil
83+
}
84+
8185
func (s *testService) Echo(str string, i int, args *echoArgs) echoResult {
8286
return echoResult{str, i, args}
8387
}

0 commit comments

Comments
 (0)