improve openapi implements

This commit is contained in:
John Guo
2021-10-25 21:23:47 +08:00
parent 2481435829
commit e0674ee7fe
2 changed files with 49 additions and 2 deletions

View File

@ -43,7 +43,7 @@ type getResponseSchemaRefInput struct {
}
func (oai *OpenApiV3) getResponseSchemaRef(in getResponseSchemaRefInput) (*SchemaRef, error) {
if oai.Config.CommonResponse == nil {
if in.ResponseObject == nil {
return &SchemaRef{
Ref: in.BusinessStructName,
}, nil

View File

@ -627,7 +627,7 @@ func TestOpenApiV3_HtmlResponse(t *testing.T) {
Id uint `json:"id" v:"min:1#请选择查看的内容" dc:"内容id"`
}
type Res struct {
g.Meta `mime:"text/html" type:"string"`
g.Meta `mime:"text/html" type:"string" example:"<html/>"`
}
f := func(ctx context.Context, req *Req) (res *Res, err error) {
@ -650,3 +650,50 @@ func TestOpenApiV3_HtmlResponse(t *testing.T) {
t.Assert(oai.Components.Schemas[`github.com.gogf.gf.v2.protocol.goai_test.Res`].Value.Type, goai.TypeString)
})
}
func TestOpenApiV3_HtmlResponseWithCommonResponse(t *testing.T) {
type CommonResError struct {
Code string `description:"错误码"`
Message string `description:"错误描述"`
}
type CommonResResponse struct {
RequestId string `description:"RequestId"`
Error *CommonResError `json:",omitempty" description:"执行错误信息"`
}
type CommonRes struct {
Response CommonResResponse
}
type Req struct {
g.Meta `path:"/test" method:"get" summary:"展示内容详情页面" tags:"内容"`
Id uint `json:"id" v:"min:1#请选择查看的内容" dc:"内容id"`
}
type Res struct {
g.Meta `mime:"text/html" type:"string" example:"<html/>"`
}
f := func(ctx context.Context, req *Req) (res *Res, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
oai.Config.CommonResponse = CommonRes{}
oai.Config.CommonResponseDataField = `Response.`
err = oai.Add(goai.AddInput{
Path: "/test",
Method: goai.HttpMethodGet,
Object: f,
})
t.AssertNil(err)
//fmt.Println(oai.String())
t.Assert(oai.Components.Schemas[`github.com.gogf.gf.v2.protocol.goai_test.Res`].Value.Type, goai.TypeString)
})
}