Skip to content

Commit 2acf375

Browse files
authored
Merge pull request #107 from LesnyRumcajs/master
fix: make resp result/error conformant to spec
2 parents 5b96398 + b0ce36a commit 2acf375

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Diff for: handler.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,30 @@ func (e *respError) val(errors *Errors) reflect.Value {
104104
}
105105

106106
type response struct {
107-
Jsonrpc string `json:"jsonrpc"`
108-
Result interface{} `json:"result,omitempty"`
109-
ID interface{} `json:"id"`
110-
Error *respError `json:"error,omitempty"`
107+
Jsonrpc string
108+
Result interface{}
109+
ID interface{}
110+
Error *respError
111+
}
112+
113+
func (r response) MarshalJSON() ([]byte, error) {
114+
// Custom marshal logic as per JSON-RPC 2.0 spec:
115+
// > `result`:
116+
// > This member is REQUIRED on success.
117+
// > This member MUST NOT exist if there was an error invoking the method.
118+
//
119+
// > `error`:
120+
// > This member is REQUIRED on error.
121+
// > This member MUST NOT exist if there was no error triggered during invocation.
122+
data := make(map[string]interface{})
123+
data["jsonrpc"] = r.Jsonrpc
124+
data["id"] = r.ID
125+
if r.Error != nil {
126+
data["error"] = r.Error
127+
} else {
128+
data["result"] = r.Result
129+
}
130+
return json.Marshal(data)
111131
}
112132

113133
type handler struct {

Diff for: rpc_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ func TestRawRequests(t *testing.T) {
127127
}
128128
}
129129

130-
t.Run("inc", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1, 200))
131-
t.Run("inc-null", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1, 200))
132-
t.Run("inc-noparam", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}`, `{"jsonrpc":"2.0","id":2}`, 1, 200))
133-
t.Run("add", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}`, `{"jsonrpc":"2.0","id":4}`, 10, 200))
130+
t.Run("inc", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}`, `{"jsonrpc":"2.0","id":1,"result":null}`, 1, 200))
131+
t.Run("inc-null", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}`, `{"jsonrpc":"2.0","id":1,"result":null}`, 1, 200))
132+
t.Run("inc-noparam", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}`, `{"jsonrpc":"2.0","id":2,"result":null}`, 1, 200))
133+
t.Run("add", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}`, `{"jsonrpc":"2.0","id":4,"result":null}`, 10, 200))
134134
// Batch requests
135135
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 5}`, `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`, 0, 500))
136-
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 6}]`, `[{"jsonrpc":"2.0","id":6}]`, 123, 200))
137-
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 7},{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-122], "id": 8}]`, `[{"jsonrpc":"2.0","id":7},{"jsonrpc":"2.0","id":8}]`, 1, 200))
138-
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 9},{"jsonrpc": "2.0", "params": [-122], "id": 10}]`, `[{"jsonrpc":"2.0","id":9},{"error":{"code":-32601,"message":"method '' not found"},"id":10,"jsonrpc":"2.0"}]`, 123, 200))
139-
t.Run("add", tc(` [{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-1], "id": 11}] `, `[{"jsonrpc":"2.0","id":11}]`, -1, 200))
136+
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 6}]`, `[{"jsonrpc":"2.0","id":6,"result":null}]`, 123, 200))
137+
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 7},{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-122], "id": 8}]`, `[{"jsonrpc":"2.0","id":7,"result":null},{"jsonrpc":"2.0","id":8,"result":null}]`, 1, 200))
138+
t.Run("add", tc(`[{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [123], "id": 9},{"jsonrpc": "2.0", "params": [-122], "id": 10}]`, `[{"jsonrpc":"2.0","id":9,"result":null},{"error":{"code":-32601,"message":"method '' not found"},"id":10,"jsonrpc":"2.0"}]`, 123, 200))
139+
t.Run("add", tc(` [{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [-1], "id": 11}] `, `[{"jsonrpc":"2.0","id":11,"result":null}]`, -1, 200))
140140
t.Run("add", tc(``, `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid request"}}`, 0, 400))
141141
}
142142

0 commit comments

Comments
 (0)