mirror of
https://github.com/beego/bee.git
synced 2025-05-12 11:21:27 +00:00
feat: support struct response with @Failture
This commit is contained in:
parent
ef570ffb7b
commit
f6f26bff3f
@ -695,23 +695,49 @@ 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"):])
|
||||||
rs := swagger.Response{}
|
rs := swagger.Response{}
|
||||||
st := strings.TrimSpace(t[len("@Failure"):])
|
respCode, pos := peekNextSplitString(ss)
|
||||||
var cd []rune
|
ss = strings.TrimSpace(ss[pos:])
|
||||||
var start bool
|
respType, pos := peekNextSplitString(ss)
|
||||||
for i, s := range st {
|
if respType == "{object}" || respType == "{array}" {
|
||||||
if unicode.IsSpace(s) {
|
isArray := respType == "{array}"
|
||||||
if start {
|
ss = strings.TrimSpace(ss[pos:])
|
||||||
rs.Description = strings.TrimSpace(st[i+1:])
|
schemaName, pos := peekNextSplitString(ss)
|
||||||
break
|
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 {
|
} else {
|
||||||
continue
|
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)
|
||||||
}
|
}
|
||||||
start = true
|
if isArray {
|
||||||
cd = append(cd, s)
|
rs.Schema = &swagger.Schema{
|
||||||
|
Type: astTypeArray,
|
||||||
|
Items: &schema,
|
||||||
}
|
}
|
||||||
opts.Responses[string(cd)] = rs
|
} else {
|
||||||
|
rs.Schema = &schema
|
||||||
|
}
|
||||||
|
rs.Description = strings.TrimSpace(ss[pos:])
|
||||||
|
} else {
|
||||||
|
rs.Description = strings.TrimSpace(ss)
|
||||||
|
}
|
||||||
|
opts.Responses[respCode] = rs
|
||||||
} else if strings.HasPrefix(t, "@Deprecated") {
|
} else if strings.HasPrefix(t, "@Deprecated") {
|
||||||
opts.Deprecated, _ = strconv.ParseBool(strings.TrimSpace(t[len("@Deprecated"):]))
|
opts.Deprecated, _ = strconv.ParseBool(strings.TrimSpace(t[len("@Deprecated"):]))
|
||||||
} else if strings.HasPrefix(t, "@Accept") {
|
} else if strings.HasPrefix(t, "@Accept") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user