From 17904bccd0aec789b03b435a0ac9d50426ce2628 Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 8 Oct 2021 21:23:30 +0800 Subject: [PATCH] openapi --- protocol/goai/goai_path.go | 17 ++++++----------- protocol/goai/goai_test.go | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/protocol/goai/goai_path.go b/protocol/goai/goai_path.go index 9553b8722..a4743a1a1 100644 --- a/protocol/goai/goai_path.go +++ b/protocol/goai/goai_path.go @@ -88,7 +88,6 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { inputMetaMap = gmeta.Data(inputObject.Interface()) outputMetaMap = gmeta.Data(outputObject.Interface()) isInputStructEmpty = oai.doesStructHasNoFields(inputObject.Interface()) - isOutputStructEmpty = oai.doesStructHasNoFields(outputObject.Interface()) inputStructTypeName = golangTypeToSchemaName(inputObject.Type()) outputStructTypeName = golangTypeToSchemaName(outputObject.Type()) operation = Operation{ @@ -211,16 +210,12 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { contentTypes = gstr.SplitAndTrim(tagMimeValue, ",") } for _, v := range contentTypes { - if isOutputStructEmpty { - response.Content[v] = MediaType{} - } else { - schemaRef, err := oai.getResponseSchemaRef(outputStructTypeName) - if err != nil { - return err - } - response.Content[v] = MediaType{ - Schema: schemaRef, - } + schemaRef, err := oai.getResponseSchemaRef(outputStructTypeName) + if err != nil { + return err + } + response.Content[v] = MediaType{ + Schema: schemaRef, } } operation.Responses[responseOkKey] = ResponseRef{Value: &response} diff --git a/protocol/goai/goai_test.go b/protocol/goai/goai_test.go index 0a81eef86..b6a34fc00 100644 --- a/protocol/goai/goai_test.go +++ b/protocol/goai/goai_test.go @@ -233,3 +233,42 @@ func TestOpenApiV3_CommonResponse(t *testing.T) { t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties), 3) }) } + +func TestOpenApiV3_CommonResponse_EmptyResponse(t *testing.T) { + type CommonResponse struct { + Code int `json:"code" description:"Error code"` + Message string `json:"message" description:"Error message"` + Data interface{} `json:"data" description:"Result data for certain request according API definition"` + } + + type Req struct { + gmeta.Meta `method:"GET"` + Product string `json:"product" in:"query" v:"required" description:"Unique product key"` + Name string `json:"name" in:"query" v:"required" description:"Instance name"` + } + type Res struct{} + + 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 = CommonResponse{} + oai.Config.CommonResponseDataField = `Data` + + err = oai.Add(goai.AddInput{ + Path: "/index", + Object: f, + }) + t.AssertNil(err) + // Schema asserts. + t.Assert(len(oai.Components.Schemas), 2) + t.Assert(len(oai.Paths), 1) + t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties), 3) + }) +}