Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 0d6192f

Browse files
authored
Allow JSON-RPC id to be Null, Number, or String (#345)
1 parent 376d4c5 commit 0d6192f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pkg/jsonrpc/handler.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"golang.org/x/text/language"
1515
)
1616

17-
func jsonrpcError(c *gin.Context, code int, message string, data any, id *float64) {
17+
func jsonrpcError(c *gin.Context, code int, message string, data any, id any) {
1818
c.JSON(http.StatusOK, gin.H{
1919
"jsonrpc": "2.0",
2020
"error": gin.H{
@@ -27,6 +27,18 @@ func jsonrpcError(c *gin.Context, code int, message string, data any, id *float6
2727
c.Abort()
2828
}
2929

30+
// parseRequestId checks if the JSON-RPC request contains an id field that is either NULL, Number, or String.
31+
func parseRequestId(data map[string]any) (any, bool) {
32+
id, ok := data["id"]
33+
_, isFloat64 := id.(float64)
34+
_, isStr := id.(string)
35+
36+
if ok && (id == nil || isFloat64 || isStr) {
37+
return id, true
38+
}
39+
return nil, false
40+
}
41+
3042
// Controller returns a custom Gin middleware that handles incoming JSON-RPC requests via HTTP. It maps the
3143
// RPC method name to struct methods on the given api. For example, if the RPC request has the method field
3244
// set to "namespace_methodName" then the controller will make a call to api.Namespace_methodName with the
@@ -58,7 +70,7 @@ func Controller(api interface{}) gin.HandlerFunc {
5870
return
5971
}
6072

61-
id, ok := data["id"].(float64)
73+
id, ok := parseRequestId(data)
6274
if !ok {
6375
jsonrpcError(c, -32600, "Invalid Request", "No or invalid 'id' in request", nil)
6476
return

0 commit comments

Comments
 (0)