Skip to content

Commit

Permalink
update swagger:route parse logic
Browse files Browse the repository at this point in the history
  • Loading branch information
waltcow committed Nov 1, 2022
1 parent f9f47ea commit 468c159
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,48 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
handlerDoc = &strings.Builder{}

if cfg.AnnotateWithSwagger {
handlerDoc.WriteString(fmt.Sprintf("// swagger:route %s %s %s %s \n", route.Method, route.Path, group.GetAnnotation("group"), strings.TrimSuffix(handler, "Handler")))
var swaggerDescription string
var swaggerSummary string
var swaggerTags []string
var swaggerOperationId string

if value, ok := route.AtDoc.Properties["description"]; ok {
swaggerDescription = value
} else if value, ok := route.AtDoc.Properties["desc"]; ok {
swaggerDescription = value
} else {
swaggerDescription = strings.TrimPrefix(route.HandlerDoc[0], "//")
}

if value, ok := route.AtDoc.Properties["summary"]; ok {
swaggerSummary = value
} else if value, ok := route.AtDoc.Properties["sum"]; ok {
swaggerSummary = value
} else {
swaggerSummary = strings.TrimPrefix(route.HandlerDoc[0], "//")
}

if value, ok := route.AtDoc.Properties["tags"]; ok {
swaggerTags = strings.Split(value, ",")
} else if value, ok := route.AtDoc.Properties["tag"]; ok {
swaggerTags = []string{value}
} else {
swaggerTags = []string{group.GetAnnotation("group")}
}

if value, ok := route.AtDoc.Properties["operationId"]; ok {
swaggerOperationId = value
} else {
swaggerOperationId = strings.TrimSuffix(handler, "Handler")
}

swaggerTagValue := strings.Join(swaggerTags, " ")
//swagger:route [method] [path pattern] [?tag1 tag2 tag3] [operation id]
handlerDoc.WriteString(fmt.Sprintf("// swagger:route %s %s %s %s \n", route.Method, route.Path, swaggerTagValue, swaggerOperationId))
handlerDoc.WriteString("//\n")
handlerDoc.WriteString(fmt.Sprintf("// %s\n", route.AtDoc.Properties["summary"]))
handlerDoc.WriteString(fmt.Sprintf("// %s\n", swaggerDescription))
handlerDoc.WriteString("//\n")
handlerDoc.WriteString(fmt.Sprintf("// %s\n", route.AtDoc.Properties["description"]))
handlerDoc.WriteString(fmt.Sprintf("// %s\n", swaggerSummary))
handlerDoc.WriteString("//\n")

// HasRequest
Expand Down

0 comments on commit 468c159

Please sign in to comment.