Skip to content

Commit

Permalink
Fix empty params false error
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 14, 2024
1 parent 726daee commit 24d8885
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,19 @@ func isNil(i any) bool {
return i == nil || reflect.ValueOf(i).IsNil()
}

func isNilOrEmpty(i any) (bool, error) {
if isNil(i) {
return true, nil
}

switch reflect.TypeOf(i).Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return reflect.ValueOf(i).Len() == 0, nil
default:
return false, fmt.Errorf("impossible param type: check request.isSane")

Check warning on line 438 in jsonrpc/server.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/server.go#L437-L438

Added lines #L437 - L438 were not covered by tests
}
}

func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, http.Header, error) {
s.log.Tracew("Received request", "req", req)

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

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

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

if isNil(params) {
isNilOrEmpty, err := isNilOrEmpty(params)
if err != nil {
return nil, err

Check warning on line 517 in jsonrpc/server.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/server.go#L517

Added line #L517 was not covered by tests
}

if isNilOrEmpty {
allParamsAreOptional := utils.All(method.Params, func(p Parameter) bool {
return p.Optional
})
Expand Down

0 comments on commit 24d8885

Please sign in to comment.