Skip to content

Commit

Permalink
fix: optimize swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Mar 30, 2023
1 parent f024f35 commit 0c3d261
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
8 changes: 4 additions & 4 deletions rest/httpx/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ var (

// Parse parses the request.
func Parse(r *http.Request, v any, isValidate bool) error {
if err := ParsePath(r, v); err != nil {
if err := ParseJsonBody(r, v); err != nil {
return err
}

if err := ParseForm(r, v); err != nil {
if err := ParsePath(r, v); err != nil {
return err
}

if err := ParseHeaders(r, v); err != nil {
if err := ParseForm(r, v); err != nil {
return err
}

if err := ParseJsonBody(r, v); err != nil {
if err := ParseHeaders(r, v); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion tools/goctl/api/gogen/gendockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gogen

import (
_ "embed"
"fmt"
"strings"

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
Expand All @@ -23,7 +24,7 @@ func genDockerfile(dir string, api *spec.ApiSpec, g *GenContext) error {
builtinTemplate: dockerfileTemplate,
data: map[string]string{
"serviceName": strings.ToLower(service.Name),
"port": string(g.Port),
"port": fmt.Sprint(g.Port),
"imageTag": "golang:1.20.2-alpine3.17",
},
})
Expand Down
29 changes: 19 additions & 10 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,38 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
var handlerDoc *strings.Builder
handlerDoc = &strings.Builder{}

handlerDoc.WriteString(fmt.Sprintf("// swagger:route %s %s %s %s \n", route.Method, route.Path, group.GetAnnotation("group"), strings.TrimSuffix(handler, "Handler")))
var isParameterRequest bool
if route.RequestType != nil && strings.Contains(strings.Join(route.RequestType.Documents(), ""),
"swagger:parameters") {
isParameterRequest = true
} else {
isParameterRequest = false
}

var swaggerPath string
if strings.Contains(route.Path, ":") {
swaggerPath = ConvertRoutePathToSwagger(route.Path)
} else {
swaggerPath = route.Path
}

handlerDoc.WriteString(fmt.Sprintf("// swagger:route %s %s %s %s \n", route.Method, swaggerPath,
group.GetAnnotation("group"), strings.TrimSuffix(handler, "Handler")))
handlerDoc.WriteString("//\n")
handlerDoc.WriteString(fmt.Sprintf("%s\n", strings.Join(route.HandlerDoc, " ")))
handlerDoc.WriteString("//\n")
handlerDoc.WriteString(fmt.Sprintf("%s\n", strings.Join(route.HandlerDoc, " ")))
handlerDoc.WriteString("//\n")

// HasRequest
if len(route.RequestTypeName()) > 0 {
var inVal string
if route.Method == "get" {
inVal = "query"
} else {
inVal = "body"
}

if len(route.RequestTypeName()) > 0 && !isParameterRequest {
handlerDoc.WriteString(fmt.Sprintf(`// Parameters:
// + name: body
// require: true
// in: %s
// type: %s
//
`, inVal, route.RequestTypeName()))
`, "body", route.RequestTypeName()))
}
// HasResp
if len(route.ResponseTypeName()) > 0 {
Expand Down
18 changes: 16 additions & 2 deletions tools/goctl/api/gogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"os"
"strings"
"text/template"

Expand Down Expand Up @@ -36,7 +37,9 @@ func genFile(c fileGenConfig) error {
if !created {
return nil
}
defer fp.Close()
defer func(fp *os.File) {
_ = fp.Close()
}(fp)

var text string
if len(c.category) == 0 || len(c.templateFile) == 0 {
Expand All @@ -63,7 +66,7 @@ func genFile(c fileGenConfig) error {
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, doc spec.Doc, indent int) error {
// write doc for swagger
for _, v := range doc {
fmt.Fprintf(writer, "\t%s\n", v)
_, _ = fmt.Fprintf(writer, "\t%s\n", v)
}

util.WriteIndent(writer, indent)
Expand Down Expand Up @@ -189,3 +192,14 @@ func golangExpr(ty spec.Type, pkg ...string) string {

return ""
}

// ConvertRoutePathToSwagger converts route path to swagger format.
func ConvertRoutePathToSwagger(data string) string {
splitData := strings.Split(data, "/")
for i, v := range splitData {
if strings.Contains(v, ":") {
splitData[i] = "{" + v[1:] + "}"
}
}
return strings.Join(splitData, "/")
}
31 changes: 31 additions & 0 deletions tools/goctl/api/gogen/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gogen

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestConvertRoutePathToSwagger(t *testing.T) {
type args struct {
data string
}
tests := []struct {
name string
args args
want string
}{
{
name: "testPath",
args: args{
data: "/init/test/:name/:age",
},
want: "/init/test/{name}/{age}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ConvertRoutePathToSwagger(tt.args.data), "ConvertRoutePathToSwagger(%v)", tt.args.data)
})
}
}

0 comments on commit 0c3d261

Please sign in to comment.