meteran / gnext

Web Framework extension for Gin. Offers the API structuring, automates validation and generates documentation. It's fully compatible with the current Gin usages and Gin's middlewares.
https://pkg.go.dev/github.com/meteran/gnext
MIT License
69 stars 5 forks source link

Getting reflect: call of reflect.Value.Elem on slice Value on invalid request #30

Closed giuliano-macedo closed 1 year ago

giuliano-macedo commented 1 year ago

Hello, firstly, great job in the library, I was looking for a fastapi-like lib for go and this one is the best!

I tried making an invalid request using the example in the README.md however I get a 500 with a 'reflect.Value.Elem' error in the logs:

example.go:

package main

import "github.com/meteran/gnext"

func main() {
    r := gnext.Router()

    r.POST("/example", handler)
    _ = r.Run()
}

type MyRequest struct {
    Id   int    `json:"id" binding:"required"`
    Name string `json:"name"`
}

type MyResponse struct {
    Result string `json:"result"`
}

func handler(req *MyRequest) *MyResponse {
    return &MyResponse{Result: req.Name}
}

curl response:

curl -vvv --request POST http://localhost:8080/example --data '{"name": "some name"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* processing: http://localhost:8080/example
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> POST /example HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
> Content-Length: 21
> Content-Type: application/x-www-form-urlencoded
> 
< HTTP/1.1 500 Internal Server Error
< Date: Sat, 26 Aug 2023 16:23:26 GMT
< Content-Length: 0
< 
* Connection #0 to host localhost left intact

logs:

[GIN] 2023/08/26 - 13:16:21 | 500 |    2.728764ms |             ::1 | POST     "/example"

2023/08/26 13:23:26 [Recovery] 2023/08/26 - 13:23:26 panic recovered:
POST /example HTTP/1.1
Host: localhost:8080
Accept: */*
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
User-Agent: curl/8.2.1

reflect: call of reflect.Value.Elem on slice Value
/usr/lib/go/src/reflect/value.go:1271 (0x4a0c12)
        Value.Elem: panic(&ValueError{"reflect.Value.Elem", v.kind()})
/home/giuliano/go/pkg/mod/github.com/meteran/gnext@v0.10.0/handler_wrapper.go:359 (0x90b259)
        (*HandlerWrapper).requestHandler: errorHandlerCaller, exists := w.errorHandlerCallers[context.error.Elem().Type()]
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8bac99)
        (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/recovery.go:102 (0x8bac87)
        CustomRecoveryWithWriter.func1: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8b9e3d)
        (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/logger.go:240 (0x8b9e0c)
        LoggerWithConfig.func1: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8b8f1a)
        (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 (0x8b8bad)
        (*Engine).handleHTTPRequest: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 (0x8b86dc)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go/src/net/http/server.go:2938 (0x6bce2d)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/net/http/server.go:2009 (0x6b8d13)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/runtime/asm_amd64.s:1650 (0x46cbe0)
        goexit: BYTE    $0x90   // NOP

go version: go1.21.0 linux/amd64 gnext version: v0.10.0 uname -a: 5.15.125-1-MANJARO #1 SMP PREEMPT Wed Aug 9 06:31:14 UTC 2023 x86_64 GNU/Linux

meteran commented 1 year ago

Hi @giuliano-oliveira, thanks for the interest and the bug report. I've created a patch for it. Will be released today with version 0.10.1.

meteran commented 1 year ago

Hi @giuliano-oliveira, sorry it took so much time, but I was waiting for approval from other contributors. The release v0.10.1 is now available. Please upgrade and verify if it solves your issue. If so - I will close this conversation.

giuliano-macedo commented 1 year ago

Thanks!, but the issue persists, is there something wrong with the release?

[giuliano@i5-10300h gnext-example]$ go get github.com/meteran/gnext@v0.10.1
[giuliano@i5-10300h gnext-example]$ go run example.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /example                  --> github.com/meteran/gnext.(*HandlerWrapper).requestHandler-fm (3 handlers)
[GIN-debug] GET    /docs.yaml                --> github.com/meteran/gnext/docs.(*Handler).YamlFile-fm (3 handlers)
[GIN-debug] GET    /docs.json                --> github.com/meteran/gnext/docs.(*Handler).JsonFile-fm (3 handlers)
[GIN-debug] GET    /docs                     --> github.com/meteran/gnext/docs.(*Handler).Docs-fm (3 handlers)
2023/09/11 09:50:18 starting server on http://localhost:8080

2023/09/11 09:50:21 [Recovery] 2023/09/11 - 09:50:21 panic recovered:
POST /example HTTP/1.1
Host: localhost:8080
Accept: */*
Content-Length: 21
Content-Type: application/x-www-form-urlencoded
User-Agent: curl/8.2.1

reflect: call of reflect.Value.Elem on slice Value
/usr/lib/go/src/reflect/value.go:1271 (0x4a0c12)
    Value.Elem: panic(&ValueError{"reflect.Value.Elem", v.kind()})
/home/giuliano/go/pkg/mod/github.com/meteran/gnext@v0.10.1/handler_wrapper.go:359 (0x90b259)
    (*HandlerWrapper).requestHandler: errorHandlerCaller, exists := w.errorHandlerCallers[context.error.Elem().Type()]
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8bac99)
    (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/recovery.go:102 (0x8bac87)
    CustomRecoveryWithWriter.func1: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8b9e3d)
    (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/logger.go:240 (0x8b9e0c)
    LoggerWithConfig.func1: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 (0x8b8f1a)
    (*Context).Next: c.handlers[c.index](c)
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 (0x8b8bad)
    (*Engine).handleHTTPRequest: c.Next()
/home/giuliano/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 (0x8b86dc)
    (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/lib/go/src/net/http/server.go:2938 (0x6bce2d)
    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/net/http/server.go:2009 (0x6b8d13)
    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/runtime/asm_amd64.s:1650 (0x46cbe0)
    goexit: BYTE    $0x90   // NOP

[GIN] 2023/09/11 - 09:50:21 | 500 |    1.237794ms |             ::1 | POST     "/example"

My go.mod:

module example.com/gnext

go 1.21.0

require github.com/meteran/gnext v0.10.1

require (
    github.com/bytedance/sonic v1.8.8 // indirect
    github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
    github.com/getkin/kin-openapi v0.116.0 // indirect
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/gin-gonic/gin v1.9.0 // indirect
    github.com/go-openapi/jsonpointer v0.19.6 // indirect
    github.com/go-openapi/swag v0.22.3 // indirect
    github.com/go-playground/locales v0.14.1 // indirect
    github.com/go-playground/universal-translator v0.18.1 // indirect
    github.com/go-playground/validator/v10 v10.12.0 // indirect
    github.com/goccy/go-json v0.10.2 // indirect
    github.com/invopop/yaml v0.1.0 // indirect
    github.com/josharian/intern v1.0.0 // indirect
    github.com/json-iterator/go v1.1.12 // indirect
    github.com/klauspost/cpuid/v2 v2.2.4 // indirect
    github.com/leodido/go-urn v1.2.3 // indirect
    github.com/mailru/easyjson v0.7.7 // indirect
    github.com/mattn/go-isatty v0.0.18 // indirect
    github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    github.com/modern-go/reflect2 v1.0.2 // indirect
    github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
    github.com/pelletier/go-toml/v2 v2.0.7 // indirect
    github.com/perimeterx/marshmallow v1.1.4 // indirect
    github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
    github.com/ugorji/go/codec v1.2.11 // indirect
    golang.org/x/arch v0.3.0 // indirect
    golang.org/x/crypto v0.8.0 // indirect
    golang.org/x/net v0.9.0 // indirect
    golang.org/x/sys v0.7.0 // indirect
    golang.org/x/text v0.9.0 // indirect
    google.golang.org/protobuf v1.30.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)
meteran commented 1 year ago

By my mistake I released the v0.10.0 as the new one (before merging PR), but I rolled it back and re-release it again. It definitely looks like you still have v0.10.0. I will release it again as 0.10.2.

meteran commented 1 year ago

ok, please try to update it again

giuliano-macedo commented 1 year ago

It works, thanks!

[giuliano@i5-10300h gnext-example]$ curl --request POST http://localhost:8080/example --data '{"id":"potato"}';echo
{"message":"invalid payload","details":["at position 14: invalid type of field 'id': was 'string', should be 'int'"],"success":false}
[giuliano@i5-10300h gnext-example]$ curl --request POST http://localhost:8080/example --data '{"name": "john doe"}'; echo
{"message":"validation error","details":["field validation for 'id' failed on the 'required' tag with value ''"],"success":false}
[giuliano@i5-10300h gnext-example]$ 
[giuliano@i5-10300h gnext-example]$ curl --request POST http://localhost:8080/example --data '{"id":1, "name": "john doe"}';echo
{"result":"john doe"}
meteran commented 1 year ago

:+1: I close this issue.