mirror of
https://github.com/beego/bee.git
synced 2025-05-12 16:31:26 +00:00
fix: add func parseResponse
This commit is contained in:
parent
f6f26bff3f
commit
8d4a94ab4c
@ -574,47 +574,12 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|||||||
} else if strings.HasPrefix(t, "@Summary") {
|
} else if strings.HasPrefix(t, "@Summary") {
|
||||||
opts.Summary = strings.TrimSpace(t[len("@Summary"):])
|
opts.Summary = strings.TrimSpace(t[len("@Summary"):])
|
||||||
} else if strings.HasPrefix(t, "@Success") {
|
} else if strings.HasPrefix(t, "@Success") {
|
||||||
ss := strings.TrimSpace(t[len("@Success"):])
|
respCode, rs, schemas := parseResponse(t)
|
||||||
rs := swagger.Response{}
|
if _, ok := modelsList[pkgpath+controllerName]; !ok {
|
||||||
respCode, pos := peekNextSplitString(ss)
|
modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema)
|
||||||
ss = strings.TrimSpace(ss[pos:])
|
}
|
||||||
respType, pos := peekNextSplitString(ss)
|
for k, v := range schemas {
|
||||||
if respType == "{object}" || respType == "{array}" {
|
modelsList[pkgpath+controllerName][k] = v
|
||||||
isArray := respType == "{array}"
|
|
||||||
ss = strings.TrimSpace(ss[pos:])
|
|
||||||
schemaName, pos := peekNextSplitString(ss)
|
|
||||||
if schemaName == "" {
|
|
||||||
beeLogger.Log.Fatalf("[%s.%s] Schema must follow {object} or {array}", controllerName, funcName)
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(schemaName, "[]") {
|
|
||||||
schemaName = schemaName[2:]
|
|
||||||
isArray = true
|
|
||||||
}
|
|
||||||
schema := swagger.Schema{}
|
|
||||||
if sType, ok := basicTypes[schemaName]; ok {
|
|
||||||
typeFormat := strings.Split(sType, ":")
|
|
||||||
schema.Type = typeFormat[0]
|
|
||||||
schema.Format = typeFormat[1]
|
|
||||||
} else {
|
|
||||||
m, mod, realTypes := getModel(schemaName)
|
|
||||||
schema.Ref = "#/definitions/" + m
|
|
||||||
if _, ok := modelsList[pkgpath+controllerName]; !ok {
|
|
||||||
modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema)
|
|
||||||
}
|
|
||||||
modelsList[pkgpath+controllerName][schemaName] = mod
|
|
||||||
appendModels(pkgpath, controllerName, realTypes)
|
|
||||||
}
|
|
||||||
if isArray {
|
|
||||||
rs.Schema = &swagger.Schema{
|
|
||||||
Type: astTypeArray,
|
|
||||||
Items: &schema,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rs.Schema = &schema
|
|
||||||
}
|
|
||||||
rs.Description = strings.TrimSpace(ss[pos:])
|
|
||||||
} else {
|
|
||||||
rs.Description = strings.TrimSpace(ss)
|
|
||||||
}
|
}
|
||||||
opts.Responses[respCode] = rs
|
opts.Responses[respCode] = rs
|
||||||
} else if strings.HasPrefix(t, "@Param") {
|
} else if strings.HasPrefix(t, "@Param") {
|
||||||
@ -695,47 +660,12 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|||||||
}
|
}
|
||||||
opts.Parameters = append(opts.Parameters, para)
|
opts.Parameters = append(opts.Parameters, para)
|
||||||
} else if strings.HasPrefix(t, "@Failure") {
|
} else if strings.HasPrefix(t, "@Failure") {
|
||||||
ss := strings.TrimSpace(t[len("@Failure"):])
|
respCode, rs, schemas := parseResponse(t)
|
||||||
rs := swagger.Response{}
|
if _, ok := modelsList[pkgpath+controllerName]; !ok {
|
||||||
respCode, pos := peekNextSplitString(ss)
|
modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema)
|
||||||
ss = strings.TrimSpace(ss[pos:])
|
}
|
||||||
respType, pos := peekNextSplitString(ss)
|
for k, v := range schemas {
|
||||||
if respType == "{object}" || respType == "{array}" {
|
modelsList[pkgpath+controllerName][k] = v
|
||||||
isArray := respType == "{array}"
|
|
||||||
ss = strings.TrimSpace(ss[pos:])
|
|
||||||
schemaName, pos := peekNextSplitString(ss)
|
|
||||||
if schemaName == "" {
|
|
||||||
beeLogger.Log.Fatalf("[%s.%s] Schema must follow {object} or {array}", controllerName, funcName)
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(schemaName, "[]") {
|
|
||||||
schemaName = schemaName[2:]
|
|
||||||
isArray = true
|
|
||||||
}
|
|
||||||
schema := swagger.Schema{}
|
|
||||||
if sType, ok := basicTypes[schemaName]; ok {
|
|
||||||
typeFormat := strings.Split(sType, ":")
|
|
||||||
schema.Type = typeFormat[0]
|
|
||||||
schema.Format = typeFormat[1]
|
|
||||||
} else {
|
|
||||||
m, mod, realTypes := getModel(schemaName)
|
|
||||||
schema.Ref = "#/definitions/" + m
|
|
||||||
if _, ok := modelsList[pkgpath+controllerName]; !ok {
|
|
||||||
modelsList[pkgpath+controllerName] = make(map[string]swagger.Schema)
|
|
||||||
}
|
|
||||||
modelsList[pkgpath+controllerName][schemaName] = mod
|
|
||||||
appendModels(pkgpath, controllerName, realTypes)
|
|
||||||
}
|
|
||||||
if isArray {
|
|
||||||
rs.Schema = &swagger.Schema{
|
|
||||||
Type: astTypeArray,
|
|
||||||
Items: &schema,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rs.Schema = &schema
|
|
||||||
}
|
|
||||||
rs.Description = strings.TrimSpace(ss[pos:])
|
|
||||||
} else {
|
|
||||||
rs.Description = strings.TrimSpace(ss)
|
|
||||||
}
|
}
|
||||||
opts.Responses[respCode] = rs
|
opts.Responses[respCode] = rs
|
||||||
} else if strings.HasPrefix(t, "@Deprecated") {
|
} else if strings.HasPrefix(t, "@Deprecated") {
|
||||||
@ -817,6 +747,50 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseResponse(annotation string) (respCode string, rs swagger.Response, schemas map[string]swagger.Schema) {
|
||||||
|
rs = swagger.Response{}
|
||||||
|
schemas = make(map[string]swagger.Schema)
|
||||||
|
ss := strings.TrimSpace(annotation[strings.Index(annotation, " "):])
|
||||||
|
respCode, pos := peekNextSplitString(ss)
|
||||||
|
ss = strings.TrimSpace(ss[pos:])
|
||||||
|
respType, pos := peekNextSplitString(ss)
|
||||||
|
if respType == "{object}" || respType == "{array}" {
|
||||||
|
isArray := respType == "{array}"
|
||||||
|
ss = strings.TrimSpace(ss[pos:])
|
||||||
|
schemaName, pos := peekNextSplitString(ss)
|
||||||
|
if schemaName == "" {
|
||||||
|
beeLogger.Log.Fatalf("Schema must follow {object} or {array}")
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(schemaName, "[]") {
|
||||||
|
schemaName = schemaName[2:]
|
||||||
|
isArray = true
|
||||||
|
}
|
||||||
|
schema := swagger.Schema{}
|
||||||
|
if sType, ok := basicTypes[schemaName]; ok {
|
||||||
|
typeFormat := strings.Split(sType, ":")
|
||||||
|
schema.Type = typeFormat[0]
|
||||||
|
schema.Format = typeFormat[1]
|
||||||
|
} else {
|
||||||
|
m, mod, realTypes := getModel(schemaName)
|
||||||
|
schema.Ref = "#/definitions/" + m
|
||||||
|
schemas[schemaName] = mod
|
||||||
|
appendSchemas(realTypes, schemas)
|
||||||
|
}
|
||||||
|
if isArray {
|
||||||
|
rs.Schema = &swagger.Schema{
|
||||||
|
Type: astTypeArray,
|
||||||
|
Items: &schema,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rs.Schema = &schema
|
||||||
|
}
|
||||||
|
rs.Description = strings.TrimSpace(ss[pos:])
|
||||||
|
} else {
|
||||||
|
rs.Description = strings.TrimSpace(ss)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func setParamType(para *swagger.Parameter, typ string, pkgpath, controllerName string) {
|
func setParamType(para *swagger.Parameter, typ string, pkgpath, controllerName string) {
|
||||||
isArray := false
|
isArray := false
|
||||||
paraType := ""
|
paraType := ""
|
||||||
@ -1353,6 +1327,20 @@ func appendModels(pkgpath, controllerName string, realTypes []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appendSchemas(realTypes []string, schemas map[string]swagger.Schema) {
|
||||||
|
for _, realType := range realTypes {
|
||||||
|
if realType != "" && !isBasicType(strings.TrimLeft(realType, "[]")) &&
|
||||||
|
!strings.HasPrefix(realType, astTypeMap) && !strings.HasPrefix(realType, "&") {
|
||||||
|
if _, ok := schemas[realType]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, mod, newRealTypes := getModel(realType)
|
||||||
|
schemas[realType] = mod
|
||||||
|
appendSchemas(newRealTypes, schemas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getSecurity(t string) (security map[string][]string) {
|
func getSecurity(t string) (security map[string][]string) {
|
||||||
security = make(map[string][]string)
|
security = make(map[string][]string)
|
||||||
p := getparams(strings.TrimSpace(t[len("@Security"):]))
|
p := getparams(strings.TrimSpace(t[len("@Security"):]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user