fix issue in goai for recursive attribute

This commit is contained in:
John Guo
2021-10-06 14:45:29 +08:00
parent 82d04d612b
commit 5a7555a6ec
2 changed files with 41 additions and 0 deletions

View File

@ -78,10 +78,14 @@ func (oai *OpenApiV3) doAddSchemaSingle(object interface{}) error {
reflectType = reflect.TypeOf(object)
structTypeName = gstr.SubStrFromREx(reflectType.String(), ".")
)
// Already added.
if _, ok := oai.Components.Schemas[structTypeName]; ok {
return nil
}
// Take the holder first.
oai.Components.Schemas[structTypeName] = SchemaRef{}
structFields, _ := structs.Fields(structs.FieldsInput{
Pointer: object,
RecursiveOption: structs.RecursiveOptionEmbeddedNoTag,

View File

@ -123,3 +123,40 @@ func TestOpenApiV3_Add(t *testing.T) {
t.Assert(len(oai.Paths[`/test2/{appId}`].Post.Parameters), 2)
})
}
func TestOpenApiV3_Add_Recursive(t *testing.T) {
type CategoryTreeItem struct {
Id uint `json:"id"`
ParentId uint `json:"parent_id"`
Items []*CategoryTreeItem `json:"items,omitempty"`
}
type CategoryGetTreeReq struct {
gmeta.Meta `path:"/category-get-tree" method:"GET" tags:"default"`
ContentType string `in:"query"`
}
type CategoryGetTreeRes struct {
List []*CategoryTreeItem
}
f := func(ctx context.Context, req *CategoryGetTreeReq) (res *CategoryGetTreeRes, err error) {
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
oai = goai.New()
)
err = oai.Add(goai.AddInput{
Path: "/tree",
Object: f,
})
t.AssertNil(err)
t.AssertNil(err)
// Schema asserts.
t.Assert(len(oai.Components.Schemas), 3)
t.Assert(oai.Components.Schemas[`CategoryTreeItem`].Value.Type, goai.TypeObject)
t.Assert(len(oai.Components.Schemas[`CategoryTreeItem`].Value.Properties), 3)
})
}