This commit is contained in:
John Guo
2021-10-08 21:23:30 +08:00
parent 5b2f40a454
commit 17904bccd0
2 changed files with 45 additions and 11 deletions

View File

@ -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}

View File

@ -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)
})
}