-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/goai: supports configuring multiple response status documents #3750
Comments
GF currently processes responses with only one result See: Line 239 in 6e5ce98
The status code cannot be customized and represents different meanings in some Need to respond according to these status codes
Currently GF does not support this, here is an idea: Set the server:
address: ":8199"
openapiPath: "/api.json"
swaggerPath: "/swagger" package main
import (
"context"
"net/http"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/goai"
)
type HelloCreateReq struct {
g.Meta `path:"/hello" method:"POST"`
Name string `v:"required" dc:"Your name"`
Age int `v:"required|between:1,200" dc:"Your age"`
}
type HelloCreateRes struct {
g.Meta `successStatusCode:"201" errorStatusCode:"400,500"`
ID uint64 `json:"id,string" dc:"ID"`
}
type HelloGetReq struct {
g.Meta `path:"/hello/{id}" method:"GET"`
// ID ID
ID uint64 `json:"id,string" dc:"ID" in:"path" v:"required"`
}
type HelloGetRes struct {
g.Meta `errorStatusCode:"400,500"`
ID uint64 `json:"id,string" dc:"ID"`
Name string `json:"name" dc:"Name"`
Age int `json:"age" dc:"Age"`
}
type Hello struct{}
func (c *Hello) Create(ctx context.Context, r *HelloCreateReq) (*HelloCreateRes, error) {
return &HelloCreateRes{ID: 1}, nil
}
func (c *Hello) Get(ctx context.Context, r *HelloGetReq) (*HelloGetRes, error) {
return &HelloGetRes{
ID: r.ID,
Name: "john",
Age: 18,
}, nil
}
func main() {
s := g.Server()
s.Use(ghttp.MiddlewareHandlerResponse)
s.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(
new(Hello),
)
})
//Set the response data structure
oai := s.GetOpenApi()
oai.Config.CommonResponse = ghttp.DefaultHandlerResponse{}
oai.Config.CommonResponseDataField = "Data"
//wrong response
dataResp := &goai.Schemas{}
dataResp.Set("code", goai.SchemaRef{
Value: &goai.Schema{
Type: "integer",
Format: "int32",
Title: "Business Status Code",
Description: "Business status code",
},
})
dataResp.Set("message", goai.SchemaRef{
Value: &goai.Schema{
Type: "string",
Title: "Business Status Description",
Description: "Business status description",
},
})
dataResp.Set("data", goai.SchemaRef{
Value: &goai.Schema{
Type: "object",
Title: "Business Data",
Description: "Business data",
Nullable: true,
},
})
oai.Components.Schemas.Set("bizmodel.HTTPResponse", goai.SchemaRef{
Value: &goai.Schema{
Type: "object",
Properties: dataResp,
},
})
// Wrong response status code match
oai.Components.Responses = goai.Responses{
// Maybe we need to remove the spaces from key? ? ?
http.StatusText(http.StatusBadRequest): goai.ResponseRef{
Value: &goai.Response{
Description: "BadRequest",
Content: map[string]goai.MediaType{
"application/json": {Schema: &goai.SchemaRef{Ref: "bizmodel.HTTPResponse"}},
},
},
},
http.StatusText(http.StatusNotFound): goai.ResponseRef{
Value: &goai.Response{
Description: "NotFound",
Content: map[string]goai.MediaType{
"application/json": {Schema: &goai.SchemaRef{Ref: "bizmodel.HTTPResponse"}},
},
},
},
http.StatusText(http.StatusInternalServerError): goai.ResponseRef{
Value: &goai.Response{
Description: "InternalServerError",
Content: map[string]goai.MediaType{
"application/json": {Schema: &goai.SchemaRef{Ref: "bizmodel.HTTPResponse"}},
},
},
},
}
s.Run()
}
Originally posted by @shuqingzai in #3747 (comment) |
See: #3747 |
GF 目前处理响应只有一个结果
200
See:
gf/net/goai/goai_path.go
Line 239 in 6e5ce98
无法自定义状态码,在一些
2xx
状态中代表不同含义,参考: 201 Created需要根据这些状态码响应
201
202
目前 GF 没有支持这个,有一个想法参考:
在
xxxRes.Meta
中设置successStatusCode
或errorStatusCode
标签,框架可以解析且设置对应的HTTP Status Code
文档HelloCreateRes
中设置successStatusCode
为201
,那么应该生成201
文档,也设置了400
和500
且这两个response
的文档可以在文档的components.responses
中通过HTTP Status text
匹配到,也应该设置到文档中HelloGetRes
中没有设置successStatusCode
则应该默认生成200
的文档Originally posted by @shuqingzai in #3747 (comment)
The text was updated successfully, but these errors were encountered: