support api prefix (#22) patch method (#21)

This commit is contained in:
MaxToby 2022-02-15 21:22:54 +08:00
parent bd6092ac10
commit 0aac4b39f3
7 changed files with 68 additions and 37 deletions

View File

@ -8,6 +8,8 @@ info(
type (
//注册请求结构
RegisterReq {
Username string `json:"username"`
Password string `json:"password"`
@ -35,28 +37,32 @@ type (
}
)
@server(
prefix: /api
)
service user-api {
@doc(
summary: 注册
)
@handler register
post /api/user/register (RegisterReq)
post /user/register (RegisterReq)
@doc(
summary: 登录
)
@handler login
post /api/user/login (LoginReq)
post /user/login (LoginReq)
@doc(
summary: 获取用户信息
)
@handler getUserInfo
get /api/user/:id (UserInfoReq) returns (UserInfoReply)
get /user/:id (UserInfoReq) returns (UserInfoReply)
@doc(
summary: 用户搜索
)
@handler searchUser
get /api/user/search (UserSearchReq) returns (UserInfoReply)
get /user/search (UserSearchReq) returns (UserInfoReply)
}

View File

@ -36,6 +36,7 @@
}
}
],
"requestBody": {},
"tags": [
"user-api"
]
@ -54,6 +55,7 @@
"parameters": [
{
"name": "body",
"description": "注册请求结构",
"in": "body",
"required": true,
"schema": {
@ -61,6 +63,7 @@
}
}
],
"requestBody": {},
"tags": [
"user-api"
]
@ -87,6 +90,7 @@
"type": "string"
}
],
"requestBody": {},
"tags": [
"user-api"
]
@ -112,6 +116,7 @@
"type": "string"
}
],
"requestBody": {},
"tags": [
"user-api"
]
@ -222,5 +227,10 @@
"name": "Authorization",
"in": "header"
}
}
},
"security": [
{
"apiKey": []
}
]
}

View File

@ -8,26 +8,24 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
)
var (
swaggerMapTypes = map[string]reflect.Kind{
"string": reflect.String,
"int": reflect.Int,
"int32": reflect.Int,
"uint32": reflect.Int,
"uint64": reflect.Int64,
"int64": reflect.Int64,
"[]string": reflect.Slice,
"[]int": reflect.Slice,
"[]int64": reflect.Slice,
"[]int32": reflect.Slice,
"[]uint32": reflect.Slice,
"[]uint64": reflect.Slice,
"bool": reflect.Bool,
"struct": reflect.Struct,
"float32": reflect.Float32,
"float64": reflect.Float64,
}
)
var swaggerMapTypes = map[string]reflect.Kind{
"string": reflect.String,
"int": reflect.Int,
"int32": reflect.Int,
"uint32": reflect.Int,
"uint64": reflect.Int64,
"int64": reflect.Int64,
"[]string": reflect.Slice,
"[]int": reflect.Slice,
"[]int64": reflect.Slice,
"[]int32": reflect.Slice,
"[]uint32": reflect.Slice,
"[]uint64": reflect.Slice,
"bool": reflect.Bool,
"struct": reflect.Struct,
"float32": reflect.Float32,
"float64": reflect.Float64,
}
// http://swagger.io/specification/#infoObject
type swaggerInfoObject struct {
@ -116,14 +114,20 @@ type swaggerOperationObject struct {
OperationID string `json:"operationId"`
Responses swaggerResponsesObject `json:"responses"`
Parameters swaggerParametersObject `json:"parameters,omitempty"`
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
RequestBody struct {
Content swaggerContentObject `json:"content,omitempty"`
} `json:"requestBody,omitempty"`
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Security *[]swaggerSecurityRequirementObject `json:"security,omitempty"`
ExternalDocs *swaggerExternalDocumentationObject `json:"externalDocs,omitempty"`
}
type swaggerParametersObject []swaggerParameterObject
type (
swaggerParametersObject []swaggerParameterObject
swaggerContentObject map[string]swaggerParametersObject
)
// http://swagger.io/specification/#parameterObject
type swaggerParameterObject struct {

View File

@ -73,7 +73,8 @@ func applyGenerate(p *plugin.Plugin, host string, basePath string) (*swaggerObje
func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swaggerPathsObject, requestResponseRefs refMap) {
for _, group := range groups {
for _, route := range group.Routes {
path := route.Path
path := group.GetAnnotation("prefix") + route.Path
parameters := swaggerParametersObject{}
if countParams(path) > 0 {
@ -163,12 +164,21 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
Ref: reqRef,
},
}
parameters = append(parameters, swaggerParameterObject{
parameter := swaggerParameterObject{
Name: "body",
In: "body",
Required: true,
Schema: &schema,
})
}
doc := strings.Join(route.RequestType.Documents(), ",")
doc = strings.Replace(doc, "//", "", -1)
if doc != "" {
parameter.Description = doc
}
parameters = append(parameters, parameter)
}
}
}
@ -232,6 +242,8 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
pathItemObject.Delete = operationObject
case http.MethodPut:
pathItemObject.Put = operationObject
case http.MethodPatch:
pathItemObject.Put = operationObject
}
paths[path] = pathItemObject

4
go.mod
View File

@ -3,9 +3,9 @@ module github.com/zeromicro/goctl-swagger
go 1.16
require (
github.com/davecgh/go-spew v1.1.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/urfave/cli/v2 v2.3.0
github.com/zeromicro/go-zero v1.3.0 // indirect
github.com/zeromicro/go-zero/tools/goctl v1.3.1
github.com/zeromicro/go-zero/tools/goctl v1.3.2
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)

5
go.sum
View File

@ -344,11 +344,10 @@ github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJB
github.com/zeromicro/antlr v0.0.1 h1:CQpIn/dc0pUjgGQ81y98s/NGOm2Hfru2NNio2I9mQgk=
github.com/zeromicro/antlr v0.0.1/go.mod h1:nfpjEwFR6Q4xGDJMcZnCL9tEfQRgszMwu3rDz2Z+p5M=
github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348/go.mod h1:ISU/8NuPyEpl9pa17Py9TBPetMjtsiHrb9f5XGiYbo8=
github.com/zeromicro/go-zero v1.3.0-beta/go.mod h1:Hy4o1VFAt32lXaQMbaBhoFeZjA/rJqJ4PTGNdGsURcc=
github.com/zeromicro/go-zero v1.3.0 h1:Eyn36yBtR043sm4YKmxR6eS3UA/GtZDktQ+UqIJ3Lm0=
github.com/zeromicro/go-zero v1.3.0/go.mod h1:Hy4o1VFAt32lXaQMbaBhoFeZjA/rJqJ4PTGNdGsURcc=
github.com/zeromicro/go-zero/tools/goctl v1.3.1 h1:vxglEHXkQahTXUaDx3Fm3hJo/wnbUfD4fLUIDzagDUE=
github.com/zeromicro/go-zero/tools/goctl v1.3.1/go.mod h1:NgoaFeaNoPbEoGGgexecELpOya32bqYF+3AKz95n0Sg=
github.com/zeromicro/go-zero/tools/goctl v1.3.2 h1:0ou75pfWW/iQJVrIKZGKHr1JeSdy6fqqTFqdTrZQyiI=
github.com/zeromicro/go-zero/tools/goctl v1.3.2/go.mod h1:VtRqVUytXEHox6vDmkFAY9zDkmSQI4CZYuKNi3xVCQg=
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.1/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.1/go.mod h1:OnjH4M8OnAotwaB2l9bVgZzRFKru7/ZMoS46OtKyd3Q=

View File

@ -10,7 +10,7 @@ import (
)
var (
version = "20210116"
version = "20220215"
commands = []*cli.Command{
{
Name: "swagger",