mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-08-16 10:08:12 +02:00
support responses with null result ({"result":null})
Previously, we incorrectly interpreted these as neither a request nor a response, and we printed an error for them. This is incorrect behavior per JSON-RPC 2.0 spec; responses can have a null result.
This commit is contained in:
parent
6e06d561ec
commit
d58e8cc226
3 changed files with 34 additions and 9 deletions
31
jsonrpc2.go
31
jsonrpc2.go
|
|
@ -3,6 +3,7 @@
|
|||
package jsonrpc2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
|
@ -540,15 +541,16 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
|
|||
// The presence of these fields distinguishes between the 2
|
||||
// message types.
|
||||
type msg struct {
|
||||
Method *string `json:"method"`
|
||||
Result interface{} `json:"result"`
|
||||
Error interface{} `json:"error"`
|
||||
ID interface{} `json:"id"`
|
||||
Method *string `json:"method"`
|
||||
Result anyValueWithExplicitNull `json:"result"`
|
||||
Error interface{} `json:"error"`
|
||||
}
|
||||
|
||||
var isRequest, isResponse bool
|
||||
checkType := func(m *msg) error {
|
||||
mIsRequest := m.Method != nil
|
||||
mIsResponse := m.Result != nil || m.Error != nil
|
||||
mIsResponse := m.Result.null || m.Result.value != nil || m.Error != nil
|
||||
if (!mIsRequest && !mIsResponse) || (mIsRequest && mIsResponse) {
|
||||
return errors.New("jsonrpc2: unable to determine message type (request or response)")
|
||||
}
|
||||
|
|
@ -593,6 +595,27 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
|
|||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
// anyValueWithExplicitNull is used to distinguish {} from
|
||||
// {"result":null} by anyMessage's JSON unmarshaler.
|
||||
type anyValueWithExplicitNull struct {
|
||||
null bool // JSON "null"
|
||||
value interface{}
|
||||
}
|
||||
|
||||
func (v anyValueWithExplicitNull) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(v.value)
|
||||
}
|
||||
|
||||
func (v *anyValueWithExplicitNull) UnmarshalJSON(data []byte) error {
|
||||
data = bytes.TrimSpace(data)
|
||||
if string(data) == "null" {
|
||||
*v = anyValueWithExplicitNull{null: true}
|
||||
return nil
|
||||
}
|
||||
*v = anyValueWithExplicitNull{}
|
||||
return json.Unmarshal(data, &v.value)
|
||||
}
|
||||
|
||||
var (
|
||||
errInvalidRequestJSON = errors.New("jsonrpc2: request must be either a JSON object or JSON array")
|
||||
errInvalidResponseJSON = errors.New("jsonrpc2: response must be either a JSON object or JSON array")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue