Skip to content
Open
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
6 changes: 3 additions & 3 deletions internal/jsonrpc2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (msg *Request) IsCall() bool { return msg.ID.IsValid() }

func (msg *Request) marshal(to *wireCombined) {
to.ID = msg.ID.value
to.Method = msg.Method
to.Method = &msg.Method
to.Params = msg.Params
}

Expand Down Expand Up @@ -183,10 +183,10 @@ func DecodeMessage(data []byte) (Message, error) {
if err != nil {
return nil, err
}
if msg.Method != "" {
if msg.Method != nil {
// has a method, must be a call
return &Request{
Method: msg.Method,
Method: *msg.Method,
ID: id,
Params: msg.Params,
}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/jsonrpc2/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const wireVersion = "2.0"
type wireCombined struct {
VersionTag string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Method *string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *WireError `json:"error,omitempty"`
Expand Down
18 changes: 18 additions & 0 deletions internal/jsonrpc2/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func TestWireMessage(t *testing.T) {
}
}

func TestDecodeEmptyMethodAsRequest(t *testing.T) {
msg, err := jsonrpc2.DecodeMessage([]byte(`{"jsonrpc":"2.0","id":5,"method":"","params":{}}`))
if err != nil {
t.Fatal(err)
}

req, ok := msg.(*jsonrpc2.Request)
if !ok {
t.Fatalf("decoded message is %T, want *jsonrpc2.Request", msg)
}
if req.ID.Raw() != int64(5) {
t.Fatalf("request id = %v, want 5", req.ID.Raw())
}
if req.Method != "" {
t.Fatalf("request method = %q, want empty string", req.Method)
}
}

func newNotification(method string, params any) jsonrpc2.Message {
msg, err := jsonrpc2.NewNotification(method, params)
if err != nil {
Expand Down
Loading