From e59ceba51bc76a3e9763a66f733f0e89847b225e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 13 Feb 2016 18:50:21 -0800 Subject: [PATCH] rpc: Fix json rpc to handle array and object request params. rpc/v2/json2 code has a bug where it treats all jsonrpc 2.0 request params like an 'object'. In accordance with the spec it could be both 'object' or an 'array'. Handle both cases. --- .../gorilla/rpc/v2/json2/json_test.go | 20 +++++++++++++++---- .../github.com/gorilla/rpc/v2/json2/server.go | 19 +++++++++--------- vendor/vendor.json | 4 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/vendor/github.com/gorilla/rpc/v2/json2/json_test.go b/vendor/github.com/gorilla/rpc/v2/json2/json_test.go index f3ed2c64b..d3c192ec2 100644 --- a/vendor/github.com/gorilla/rpc/v2/json2/json_test.go +++ b/vendor/github.com/gorilla/rpc/v2/json2/json_test.go @@ -71,7 +71,7 @@ type Service1Request struct { B int } -type Service1BadRequest struct { +type Service1NoParamsRequest struct { V string `json:"jsonrpc"` M string `json:"method"` ID uint64 `json:"id"` @@ -84,8 +84,15 @@ type Service1Response struct { type Service1 struct { } +const Service1DefaultResponse = 9999 + func (t *Service1) Multiply(r *http.Request, req *Service1Request, res *Service1Response) error { - res.Result = req.A * req.B + if req.A == 0 && req.B == 0 { + // Sentinel value for test with no params. + res.Result = Service1DefaultResponse + } else { + res.Result = req.A * req.B + } return nil } @@ -139,8 +146,13 @@ func TestService(t *testing.T) { t.Errorf("Expected to get %q, but got %q", ErrResponseError, err) } - if err := executeRaw(t, s, &Service1BadRequest{"2.0", "Service1.Multiply", 1}, &res); err == nil { - t.Errorf("Expected error but error in nil") + // No parameters. + res = Service1Response{} + if err := executeRaw(t, s, &Service1NoParamsRequest{"2.0", "Service1.Multiply", 1}, &res); err != nil { + t.Error(err) + } + if res.Result != Service1DefaultResponse { + t.Errorf("Wrong response: got %v, want %v", res.Result, Service1DefaultResponse) } } diff --git a/vendor/github.com/gorilla/rpc/v2/json2/server.go b/vendor/github.com/gorilla/rpc/v2/json2/server.go index f04f44a18..e2266b4a5 100644 --- a/vendor/github.com/gorilla/rpc/v2/json2/server.go +++ b/vendor/github.com/gorilla/rpc/v2/json2/server.go @@ -125,22 +125,21 @@ func (c *CodecRequest) Method() (string, error) { // ReadRe