1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 06:21:34 +00:00

Merge pull request #263 from sergeylanzman/swagger-add-embedded-struct

Swagger add embedded struct
This commit is contained in:
astaxie 2016-08-29 22:09:11 +08:00 committed by GitHub
commit 3d7a532567

View File

@ -610,6 +610,24 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if k != objectname { if k != objectname {
continue continue
} }
parseObject(d, k, &m, &realTypes, astPkgs)
}
}
}
}
if m.Title == "" {
ColorLog("[WARN]can't find the object: %s\n", str)
// TODO remove when all type have been supported
//os.Exit(1)
}
if len(rootapi.Definitions) == 0 {
rootapi.Definitions = make(map[string]swagger.Schema)
}
rootapi.Definitions[objectname] = m
return
}
func parseObject(d *ast.Object, k string, m *swagger.Schema, realTypes *[]string, astPkgs map[string]*ast.Package) {
ts, ok := d.Decl.(*ast.TypeSpec) ts, ok := d.Decl.(*ast.TypeSpec)
if !ok { if !ok {
ColorLog("Unknown type without TypeSec: %v\n", d) ColorLog("Unknown type without TypeSec: %v\n", d)
@ -618,16 +636,15 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
// TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc... // TODO support other types, such as `ArrayType`, `MapType`, `InterfaceType` etc...
st, ok := ts.Type.(*ast.StructType) st, ok := ts.Type.(*ast.StructType)
if !ok { if !ok {
continue return
} }
m.Title = k m.Title = k
if st.Fields.List != nil { if st.Fields.List != nil {
m.Properties = make(map[string]swagger.Propertie) m.Properties = make(map[string]swagger.Propertie)
for _, field := range st.Fields.List { for _, field := range st.Fields.List {
isSlice, realType, sType := typeAnalyser(field) isSlice, realType, sType := typeAnalyser(field)
realTypes = append(realTypes, realType) *realTypes = append(*realTypes, realType)
mp := swagger.Propertie{} mp := swagger.Propertie{}
// add type slice
if isSlice { if isSlice {
mp.Type = "array" mp.Type = "array"
if isBasicType(realType) { if isBasicType(realType) {
@ -650,8 +667,6 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
mp.Ref = "#/definitions/" + realType mp.Ref = "#/definitions/" + realType
} }
} }
// dont add property if anonymous field
if field.Names != nil { if field.Names != nil {
// set property name as field name // set property name as field name
@ -697,6 +712,12 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
if ignore := stag.Get("ignore"); ignore != "" { if ignore := stag.Get("ignore"); ignore != "" {
continue continue
} }
} else {
for _, pkg := range astPkgs {
for _, fl := range pkg.Files {
for nameOfObj, obj := range fl.Scope.Objects {
if obj.Name == fmt.Sprint(field.Type) {
parseObject(obj, nameOfObj, m, realTypes, astPkgs)
} }
} }
} }
@ -704,16 +725,6 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Schema, realTyp
} }
} }
} }
if m.Title == "" {
ColorLog("[WARN]can't find the object: %s\n", str)
// TODO remove when all type have been supported
//os.Exit(1)
}
if len(rootapi.Definitions) == 0 {
rootapi.Definitions = make(map[string]swagger.Schema)
}
rootapi.Definitions[objectname] = m
return
} }
func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) { func typeAnalyser(f *ast.Field) (isSlice bool, realType, swaggerType string) {