Skip to content

Commit 24d8885

Browse files
committed
Fix empty params false error
1 parent 726daee commit 24d8885

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

jsonrpc/server.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,19 @@ func isNil(i any) bool {
426426
return i == nil || reflect.ValueOf(i).IsNil()
427427
}
428428

429+
func isNilOrEmpty(i any) (bool, error) {
430+
if isNil(i) {
431+
return true, nil
432+
}
433+
434+
switch reflect.TypeOf(i).Kind() {
435+
case reflect.Slice, reflect.Array, reflect.Map:
436+
return reflect.ValueOf(i).Len() == 0, nil
437+
default:
438+
return false, fmt.Errorf("impossible param type: check request.isSane")
439+
}
440+
}
441+
429442
func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, http.Header, error) {
430443
s.log.Tracew("Received request", "req", req)
431444

@@ -486,6 +499,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, ht
486499
return res, header, nil
487500
}
488501

502+
//nolint:gocyclo
489503
func (s *Server) buildArguments(ctx context.Context, params any, method Method) ([]reflect.Value, error) {
490504
handlerType := reflect.TypeOf(method.Handler)
491505

@@ -498,7 +512,12 @@ func (s *Server) buildArguments(ctx context.Context, params any, method Method)
498512
addContext = 1
499513
}
500514

501-
if isNil(params) {
515+
isNilOrEmpty, err := isNilOrEmpty(params)
516+
if err != nil {
517+
return nil, err
518+
}
519+
520+
if isNilOrEmpty {
502521
allParamsAreOptional := utils.All(method.Params, func(p Parameter) bool {
503522
return p.Optional
504523
})

0 commit comments

Comments
 (0)