diff --git a/.gitignore b/.gitignore index 709e02d..747f9fa 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ Thumbs.db *.ipr # -.swagger-codegen \ No newline at end of file +.swagger-codegen +swagtest +*.exe \ No newline at end of file diff --git a/example/user.api b/example/user.api index 4a1bf59..979dc77 100644 --- a/example/user.api +++ b/example/user.api @@ -1,81 +1,81 @@ info( - title: "type title here" - desc: "type desc here" - author: "type author here" - email: "type email here" - version: "type version here" + title: "type title here" + desc: "type desc here" + author: "type author here" + email: "type email here" + version: "type version here" ) - type ( - - //注册请求结构 - RegisterReq { - Username string `json:"username"` - Password string `json:"password"` - Mobile string `json:"mobile"` - } - LoginReq { - Username string `json:"username"` //测试 - Password string `json:"password"`//测试2 - } - UserInfoReq { - Id string `path:"id"` - } + //注册请求结构 + RegisterReq { + Username string `json:"username"` + Password string `json:"password"` + Mobile string `json:"mobile"` + } + + LoginReq { + Username string `json:"username"` //测试 + Password string `json:"password"` //测试2 + AppId string `header:"appId"` //APPID-TEST + } + UserInfoReq { + Id string `path:"id"` + } - UserInfoReply { - Name string `json:"name"` - Age int `json:"age"` - Birthday string `json:"birthday"` - Description string `json:"description"` - Tag []string `json:"tag"` - Tags [][]string `json:"tags"` - } + UserInfoReply { + Name string `json:"name"` + Age int `json:"age"` + Birthday string `json:"birthday"` + Description string `json:"description"` + Tag []string `json:"tag"` + Tags [][]string `json:"tags"` + } - UserSearchReq { - KeyWord string `form:"keyWord"` // 关键词 - } + UserSearchReq { + KeyWord string `form:"keyWord"` // 关键词 + } - ErrorResponse { - Code string `json:"code"` - Message string `json:"message"` - } + ErrorResponse { + Code string `json:"code"` + Message string `json:"message"` + } ) @server( - prefix: /api + prefix: /api ) service user-api { - @doc( - summary: 注册 - ) - @handler register - post /user/register (RegisterReq) + @doc( + summary: 注册 + ) + @handler register + post /user/register (RegisterReq) - @doc( - summary: 登录 - ) - @handler login - post /user/login (LoginReq) + @doc( + summary: 登录 + ) + @handler login + post /user/login (LoginReq) - @doc( - summary: 获取用户信息 - ) - @handler getUserInfo - /* - @respdoc-400 ( - 100101: out of authority - 100102: user not exist - ) // Error code list - */ - /* @respdoc-500 (ErrorResponse) // Server Error */ - get /user/:id (UserInfoReq) returns (UserInfoReply) + @doc( + summary: 获取用户信息 + ) + @handler getUserInfo + /* + @respdoc-400 ( + 100101: out of authority + 100102: user not exist + ) // Error code list + */ + /* @respdoc-500 (ErrorResponse) // Server Error */ + get /user/:id (UserInfoReq) returns (UserInfoReply) - @doc( - summary: 用户搜索 - ) - @handler searchUser - get /user/search (UserSearchReq) returns (UserInfoReply) + @doc( + summary: 用户搜索 + ) + @handler searchUser + get /user/search (UserSearchReq) returns (UserInfoReply) } \ No newline at end of file diff --git a/generate/parser.go b/generate/parser.go index 147ee18..ea94cb2 100644 --- a/generate/parser.go +++ b/generate/parser.go @@ -162,73 +162,13 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge } if defineStruct, ok := route.RequestType.(spec.DefineStruct); ok { for _, member := range defineStruct.Members { - if member.Name == "" { - memberDefineStruct, _ := member.Type.(spec.DefineStruct) - for _, m := range memberDefineStruct.Members { - if strings.Contains(m.Tag, "header") { - tempKind := swaggerMapTypes[strings.Replace(m.Type.Name(), "[]", "", -1)] - ftype, format, ok := primitiveSchema(tempKind, m.Type.Name()) - if !ok { - ftype = tempKind.String() - format = "UNKNOWN" - } - sp := swaggerParameterObject{In: "header", Type: ftype, Format: format} - - for _, tag := range m.Tags() { - sp.Name = tag.Name - if len(tag.Options) == 0 { - sp.Required = true - continue - } - - required := true - for _, option := range tag.Options { - if strings.HasPrefix(option, optionsOption) { - segs := strings.SplitN(option, equalToken, 2) - if len(segs) == 2 { - sp.Enum = strings.Split(segs[1], optionSeparator) - } - } - - if strings.HasPrefix(option, rangeOption) { - segs := strings.SplitN(option, equalToken, 2) - if len(segs) == 2 { - min, max, ok := parseRangeOption(segs[1]) - if ok { - sp.Schema.Minimum = min - sp.Schema.Maximum = max - } - } - } - - if strings.HasPrefix(option, defaultOption) { - segs := strings.Split(option, equalToken) - if len(segs) == 2 { - sp.Default = segs[1] - } - } else if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) { - required = false - } - - if strings.HasPrefix(option, exampleOption) { - segs := strings.Split(option, equalToken) - if len(segs) == 2 { - sp.Example = segs[1] - } - } - } - sp.Required = required - } - sp.Description = strings.TrimLeft(m.Comment, "//") - parameters = append(parameters, sp) - } - } - continue + if hasHeaderParameters(member) { + parameters = parseHeader(member, parameters) } } if strings.ToUpper(route.Method) == http.MethodGet { for _, member := range defineStruct.Members { - if strings.Contains(member.Tag, "path") { + if hasPathParameters(member) || hasHeaderParameters(member) { continue } if embedStruct, isEmbed := member.Type.(spec.DefineStruct); isEmbed { @@ -483,7 +423,7 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec. schema.Title = defineStruct.Name() for _, member := range defineStruct.Members { - if hasPathParameters(member) { + if hasPathParameters(member) || hasHeaderParameters(member) { continue } kv := keyVal{Value: schemaOfField(member)} @@ -494,8 +434,7 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec. if kv.Key == "" { memberStruct, _ := member.Type.(spec.DefineStruct) for _, m := range memberStruct.Members { - - if strings.Contains(m.Tag, "header") { + if hasHeaderParameters(m) || hasPathParameters(m) { continue } @@ -560,6 +499,15 @@ func hasPathParameters(member spec.Member) bool { return false } +func hasHeaderParameters(member spec.Member) bool { + for _, tag := range member.Tags() { + if tag.Key == "header" { + return true + } + } + return false +} + func schemaOfField(member spec.Member) swaggerSchemaObject { ret := swaggerSchemaObject{} @@ -751,3 +699,73 @@ func contains(s []string, str string) bool { return false } + +func parseHeader(m spec.Member, parameters []swaggerParameterObject) []swaggerParameterObject { + + tempKind := swaggerMapTypes[strings.Replace(m.Type.Name(), "[]", "", -1)] + ftype, format, ok := primitiveSchema(tempKind, m.Type.Name()) + if !ok { + ftype = tempKind.String() + format = "UNKNOWN" + } + sp := swaggerParameterObject{In: "header", Type: ftype, Format: format} + + for _, tag := range m.Tags() { + sp.Name = tag.Name + if len(tag.Options) == 0 { + sp.Required = true + continue + } + + required := true + for _, option := range tag.Options { + if strings.HasPrefix(option, optionsOption) { + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + sp.Enum = strings.Split(segs[1], optionSeparator) + } + } + + if strings.HasPrefix(option, rangeOption) { + segs := strings.SplitN(option, equalToken, 2) + if len(segs) == 2 { + min, max, ok := parseRangeOption(segs[1]) + if ok { + sp.Schema.Minimum = min + sp.Schema.Maximum = max + } + } + } + + if strings.HasPrefix(option, defaultOption) { + segs := strings.Split(option, equalToken) + if len(segs) == 2 { + sp.Default = segs[1] + } + } else if strings.HasPrefix(option, optionalOption) || strings.HasPrefix(option, omitemptyOption) { + required = false + } + + if strings.HasPrefix(option, exampleOption) { + segs := strings.Split(option, equalToken) + if len(segs) == 2 { + sp.Example = segs[1] + } + } + } + sp.Required = required + } + sp.Description = strings.TrimLeft(m.Comment, "//") + if m.Name == "" { + memberDefineStruct, ok := m.Type.(spec.DefineStruct) + if !ok { + return parameters + } + for _, cm := range memberDefineStruct.Members { + if hasHeaderParameters(cm) { + parameters = parseHeader(cm, parameters) + } + } + } + return append(parameters, sp) +} diff --git a/swagtest b/swagtest deleted file mode 100755 index a83422e..0000000 Binary files a/swagtest and /dev/null differ