diff --git a/console/commands/swagger.go b/console/commands/swagger.go index 8248bd1..64e2915 100644 --- a/console/commands/swagger.go +++ b/console/commands/swagger.go @@ -113,12 +113,15 @@ func (SwaggerCommand) Execute(input command.Input) { for _, service := range fileParser.Services { var prefix string if routeGroup, ok := service.Opt["http.RouteGroup"]; ok { - prefix = "/$[" + routeGroup.Val + "]" + prefix = "$[" + routeGroup.Val + "]" if routeGroup.Doc != "" { - re := regexp.MustCompile(`(?i)@prefix=(\w+)`) + re := regexp.MustCompile(`(?i)@prefix=(.*)`) match := re.FindStringSubmatch(routeGroup.Doc) if match != nil { - prefix = "/" + match[1] + prefix = match[1] + if !strings.HasPrefix(prefix, "/") { + prefix = "/" + prefix + } } } } @@ -334,6 +337,7 @@ func messageToParameters(message string, nowDirProtoc []parser.ProtocFileParser, for _, option := range protocMessage.Attr { doc, isRequired := filterRequired(option.Doc) doc, example := filterExample(doc, option.Ty) + doc = filterLanguage(doc) doc = getTitle(doc) if option.Repeated { if isProtoBaseType(option.Ty) { @@ -652,8 +656,8 @@ func filterRequired(doc string) (string, bool) { re := regexp.MustCompile(`(?i)[/\s]*@tag\("binding"[,\s"]+([^"]+)"\)\s*`) re2 := regexp.MustCompile(`(?i)required`) for _, s := range arr { - match := re.FindStringSubmatch(s) - if len(match) == 2 && re2.MatchString(arr[1]) { + matches := re.FindStringSubmatch(s) + if len(matches) == 2 && re2.MatchString(matches[1]) { isRequired = true } else { newArr = append(newArr, s) @@ -772,6 +776,8 @@ func parseParamInPath(option parser.Option) (params openapi.Parameters) { case "lang": if m[2] == language { correctLang = true + } else { + correctDoc = false } case "format": if correctDoc { diff --git a/docs/swagger.md b/docs/swagger.md index a04d6c6..e084858 100644 --- a/docs/swagger.md +++ b/docs/swagger.md @@ -10,7 +10,7 @@ ### 2024-10-18 更新 -1. 在路由组声明上一行加上注释`@prefix=xxx`即可指定当前protobuf文件生成的swagger的接口前缀为`xxx`,该注释仅文档使用,应与业务代码一致) +1. 在路由组声明上一行加上注释`@prefix=xxx`即可指定当前protobuf文件生成的swagger的接口前缀为`xxx`,该注释仅文档使用,应与业务代码一致 ``` // @prefix=api option (http.RouteGroup) = "api"; diff --git a/parser/protoc.go b/parser/protoc.go index 9dccb87..358887c 100644 --- a/parser/protoc.go +++ b/parser/protoc.go @@ -167,7 +167,7 @@ func IsSpaceAndEspecially(r rune) bool { // This property isn't the same as Z; special-case it. if uint32(r) <= unicode.MaxLatin1 { switch r { - case '=', ';', '/', '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0: + case ';', '/', '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0: return true } return false