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: 5 additions & 1 deletion internal/jsonrpc2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ func DecodeMessage(data []byte) (Message, error) {
if err != nil {
return nil, err
}
if msg.Method != "" {
var fields map[string]json.RawMessage
if err := internaljson.Unmarshal(data, &fields); err != nil {
return nil, fmt.Errorf("unmarshaling jsonrpc message fields: %w", err)
}
if _, hasMethod := fields["method"]; hasMethod {
// has a method, must be a call
return &Request{
Method: msg.Method,
Expand Down
17 changes: 17 additions & 0 deletions internal/jsonrpc2/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ func TestWireMessage(t *testing.T) {
}
}

func TestDecodeMessageWithEmptyMethodIsRequest(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("DecodeMessage returned %T, want *jsonrpc2.Request", msg)
}
if req.Method != "" {
t.Fatalf("Request.Method = %q, want empty string", req.Method)
}
if got := req.ID.Raw(); got != int64(5) {
t.Fatalf("Request.ID = %v, want 5", got)
}
}

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