From e0674ee7fe366d632078163f32872edb3baa8ef2 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 25 Oct 2021 21:23:47 +0800 Subject: [PATCH] improve openapi implements --- protocol/goai/goai_response.go | 2 +- protocol/goai/goai_test.go | 49 +++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/protocol/goai/goai_response.go b/protocol/goai/goai_response.go index 42ad1aa36..a5337bda9 100644 --- a/protocol/goai/goai_response.go +++ b/protocol/goai/goai_response.go @@ -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 diff --git a/protocol/goai/goai_test.go b/protocol/goai/goai_test.go index ed5dbe6e7..b7a5cd3f2 100644 --- a/protocol/goai/goai_test.go +++ b/protocol/goai/goai_test.go @@ -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:""` } 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:""` + } + + 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) + }) +}