[gogf/gf][BUG]GF 2.1.* 版本,结构化路由,control使用结构体包含内嵌结构体字段会导致swagger出问题

2024-06-25 994 views
9
  • api使用的结构体
// MentalAnswerReq is the request struct for mental.Answer
type MentalAnswerReq struct {
    g.Meta `path:"/answer" tags:"MentalSrv" method:"post" summary:"问题回答请求结构体"`
    model.MentalAnswerInput
}

// MentalAnswerInput 问题回答请求结构体
type MentalAnswerInput struct {
    ScaleId int `v:"required" json:"scale_id"`
    Answers []struct {
        QuestionId int    `v:"required" json:"question_id"`
        OptionId   string `v:"required" json:"option_id"`
    }
}
  • control代码
func (c *cMental) Answer(ctx context.Context, req *api.MentalAnswerReq) (res *api.MentalAnswerRes, err error) {
    res = &api.MentalAnswerRes{}
    res.MentalAnswerOutput, err = service.Mental().Answer(ctx, req.MentalAnswerInput)
    return
}

自动生成swagger会失败 image

服务端报异常为:

2022-07-13 12:25:19.279 {10097eb1ec4801179d7f8017335ef310} 500 "GET http 127.0.0.1:8999 /api.json HTTP/1.1" 0.003, 127.0.0.1, "http://127.0.0.1:8999/swagger/", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49", -1, "", ""      
Stack:
1. WriteJson failed
   1).  github.com/gogf/gf/v2/net/ghttp.(*Response).WriteJson
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_response_write.go:116
   2).  github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_server_openapi.go:53
   3).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc.func1
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:131
   4).  github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_func.go:55
   5).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:129
   6).  github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.4
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:90
   7).  github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_func.go:55
   8).  github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:89
   9).  github.com/gogf/gf/v2/util/gutil.TryCatch
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/util/gutil/gutil.go:56
   10). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:49
   11). github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_middleware_tracing.go:78
   12). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:96
   13). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_func.go:55
   14). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:95
   15). github.com/gogf/gf/v2/util/gutil.TryCatch
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/util/gutil/gutil.go:56
   16). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:49
   17). github.com/gogf/gf/v2/net/ghttp.(*Server).ServeHTTP
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_server_handler.go:121
2. json.Marshal failed
   1).  github.com/gogf/gf/v2/internal/json.Marshal
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/internal/json/json.go:30
   2).  github.com/gogf/gf/v2/net/ghttp.(*Response).WriteJson
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_response_write.go:115
   3).  github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_server_openapi.go:53
   4).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc.func1
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:131
   5).  github.com/gogf/gf/v2/net/ghttp.niceCallFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_func.go:55
   6).  github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:129
   7).  github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.4
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:90
   8).  github.com/gogf/gf/v2/net/ghttp.niceCallFunc
   16). github.com/gogf/gf/v2/util/gutil.TryCatch
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/util/gutil/gutil.go:56
   17). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_request_middleware.go:49
   18). github.com/gogf/gf/v2/net/ghttp.(*Server).ServeHTTP
        D:/Myproject/MyGO/pkg/mod/github.com/gogf/gf/v2@v2.1.2/net/ghttp/ghttp_server_handler.go:121
3. json: error calling MarshalJSON for type goai.Schemas: invalid character 'v' after object key
  • 结构体定义改成如下方式就没有问题了
// Answer 用于提交答案
type Answer struct {
    QuestionId int    `v:"required" json:"question_id"`
    OptionId   string `v:"required" json:"option_id"`
}

// MentalAnswerInput 问题回答请求结构体
type MentalAnswerInput struct {
    ScaleId int `v:"required" json:"scale_id"`
    Answers []Answer
}

回答

2

@Tiller-Mu 请提供可复现该问题的最小可运行代码。

7

我也遇到了,你直接把所有结构体全部拆出去,单独命名,再引入就可以了

5

是的,目前是这样做的。

2

我也遇到了,现在的临时方法,就是拆结构体,但是有些结构体,是多层嵌套,拆分结构体导致代码很凌乱

8

@Tiller-Mu @HyunsuQT @kureCC 不好意思各位朋友,请试试最新的版本哈,这个我之前已经修复了。如果仍旧有问题,请重开issue。