Open zhqqqy opened 1 month ago
Hey @zhqqqy, thanks for going though code and noticing the problems.
The correct handler method is defined here which uses generic to avoid the problem you highlighted with any
type, along with the bind methods which check for the request type being nil, so it would only parse the body if caller is expecting the body.
The crud/router.go
file is something I was trying by defining a separate router itself rather than defining the wrapper methods for handler. This didn't work out as expected because apparently the method can use only the generic type which are defined as part of struct itself and not dynamically during method call. That's why I restructured it as wrapper methods (here, and here for crud) which takes the handler as argument and returns the gin handler. I missed to remove this file from package, it's removed now. Thanks for highlighting.
Hello, I have noticed some issues in your code.
type HandlerFunc func(*gin.Context, any)
, where one of the parameters type isany
. It seems that you believe this allows any type to be passed as the second argument in the function. However, in Go, function signatures must strictly match the type definition. Therefore, the function passed must also be of typeany
. If you pass a different type as the second argument, it will panic at the assertionhandler.(HandlerFunc)(c, req)
.bindRequest
function. You are usingShouldBindJSON
followed byShouldBindQuery
,ShouldBindUri
, andShouldBindHeader
methods. This can cause a problem. For example, if you have a GET request like/?name=test&id=1&age=19
without a request body, theShouldBindJSON
method will return an error, and the subsequent ShouldBindQuery method will not be executed.