improve openapi

This commit is contained in:
John Guo 2022-03-02 10:26:09 +08:00
parent f8067f5dd5
commit 3c58b8d7fa
3 changed files with 16 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/os/gstructs"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
// Response is specified by OpenAPI/Swagger 3.0 standard.
@ -59,10 +60,12 @@ func (oai *OpenApiV3) getResponseSchemaRef(in getResponseSchemaRefInput) (*Schem
return nil, err
}
if in.CommonResponseDataField == "" && bizResponseStructSchemaRefExist {
// Normal response.
for k, v := range bizResponseStructSchemaRef.Value.Properties {
schema.Properties[k] = v
}
} else {
// Common response.
structFields, _ := gstructs.Fields(gstructs.FieldsInput{
Pointer: in.CommonResponseObject,
RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
@ -77,14 +80,17 @@ func (oai *OpenApiV3) getResponseSchemaRef(in getResponseSchemaRefInput) (*Schem
switch len(dataFieldsPartsArray) {
case 1:
if structField.Name() == dataFieldsPartsArray[0] {
err = gconv.Struct(oai.fileMapWithShortTags(structField.TagMap()), bizResponseStructSchemaRef.Value)
if err != nil {
return nil, err
}
schema.Properties[fieldName] = bizResponseStructSchemaRef
break
}
default:
// Recursively creating common response object schema.
if structField.Name() == dataFieldsPartsArray[0] {
var (
structFieldInstance = reflect.New(structField.Type().Type).Elem()
)
var structFieldInstance = reflect.New(structField.Type().Type).Elem()
schemaRef, err := oai.getResponseSchemaRef(getResponseSchemaRefInput{
BusinessStructName: in.BusinessStructName,
CommonResponseObject: structFieldInstance,

View File

@ -142,9 +142,7 @@ func (oai *OpenApiV3) structToSchema(object interface{}) (*Schema, error) {
if !gstr.IsLetterUpper(structField.Name()[0]) {
continue
}
var (
fieldName = structField.Name()
)
var fieldName = structField.Name()
if jsonName := structField.TagJsonName(); jsonName != "" {
fieldName = jsonName
}

View File

@ -459,10 +459,16 @@ func TestOpenApiV3_CommonResponse(t *testing.T) {
Object: f,
})
t.AssertNil(err)
//g.Dump(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 3)
t.Assert(len(oai.Paths), 1)
t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties), 3)
t.Assert(
oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties["data"].Value.Description,
`Result data for certain request according API definition`,
)
})
}