Skip to content

Commit

Permalink
Merge pull request #98 from youbudong/main
Browse files Browse the repository at this point in the history
fix: header parameters
  • Loading branch information
kevwan authored Apr 3, 2024
2 parents 862371e + 8f66ac9 commit a4787d7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 130 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ Thumbs.db
*.ipr

#
.swagger-codegen
.swagger-codegen
swagtest
*.exe
126 changes: 63 additions & 63 deletions example/user.api
Original file line number Diff line number Diff line change
@@ -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)
}
150 changes: 84 additions & 66 deletions generate/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)}
Expand All @@ -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
}

Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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)
}
Binary file removed swagtest
Binary file not shown.

0 comments on commit a4787d7

Please sign in to comment.